TCL script to open multiple xterms and fire jobs in the opened xterms - linux

I want to write a script in tcl to perform the below operation.
set ss test1
if ([string compare $ss "test1"]) {
## Open new xterm
Ex:- bsub -Ip -n 1 -M 100 -q <queue_name> xterm &
## go to dir2
Ex:- cd dir2
## then source test1_file in the opened xterm
}
if ([string compare $ss "test2"]) {
## Open new xterm
Ex:- bsub -Ip -n 1 -M 100 -q <queue_name> xterm &
## go to dir2
Ex:- cd dir2
## then source test2_file
}
when I try with xterm -e "cd dir1" & in the unix terminal xterm was opened and it got killed automatically.
Please help me how to do this.
Thanks and Regards,
Gunnesh.

I'm not sure entirely, that this would help, but I use something like:
1) create run.sh containing:
#! /bin/bash
eval "$#"
echo "Press return to continue"
read
In Windows, it would be run.bat:
#echo off
%*
pause
2) to execute a command (say, "dir"), use Tcl command:
exec xterm -e ./run.sh dir
This would display a console, a result of executed command and a prompt "Press...".

Related

How to run script multiple times and after every execution of command to wait until the device is ready to execute again?

I have this bash script:
#!/bin/bash
rm /etc/stress.txt
cat /dev/smd10 | tee /etc/stress.txt &
for ((i=0; i< 1000; i++))
do
echo -e "\nRun number: $i\n"
#wait untill module restart and bee ready for next restart
dmesg | grep ERROR
echo -e 'AT+CFUN=1,1\r\n' > /dev/smd10
echo -e "\nADB device booted successfully\n"
done
I want to restart module 1000 times using this script.
Module is like android device witch has linux inside it. But I use Windows.
AT+CFUN=1,1 - reset
When I push script, after every restart I need a command which will wait module and start up again and execute script 1000 times. Then I do pull in .txt file and save all output content.
Which command should I use?
I try commands like wait, sleep, watch, adb wait-for-device, ps aux | grep... Nothing works.
Can someone help me with this?
I find the solution. This is how my script actually looks:
#!/bin/bash
cat /dev/smd10 &
TEST=$(cat /etc/output.txt)
RESTART_TIMES=1000
if [[ $TEST != $RESTART_TIMES ]]
then
echo $((TEST+1)) > /etc/output.txt
dmesg
echo -e 'AT+CFUN=1,1\r\n' > /dev/smd10
fi
These are the steps that you need to do:
adb push /path/to/your/script /etc/init.d
cd /etc
cat outputfile.txt - make an output file and write inside file 0 ( echo 0 > output.txt )
cd init.d
ls - you should see rc5.d
cd .. then cd rc5.d - go inside
ln -s ../init.d/yourscript.sh S99yourscript.sh
ls - you should see S99yourscript.sh
cd .. return to init.d directory
chmod +x yourscript.sh - add permision to your script
./yourscript.sh

Filter specific output of bash -x

I'm trying to filter specific output produced when executing bash -x command. Here is the code I have :
touch ./log/time_$now_time.txt
touch ./log/session_$now_time.log
case $multinode in
true)whiptail --title "Multinode system" --msgbox "Multinode system found! Redirecting to the Multinode Menu... " 10 60
cd multinode
script -q -t 2> ../log/time_$now_time.txt -c "bash -x ./menu.sh" -f ../log/session_$now_time.log ;;
false)whiptail --title "Singlenode system" --msgbox "Singlenode system found!Redirecting to the Singlenode Menu..." 10 60
cd singlenode
script -q -t 2> ../log/time_$now_time.txt -c "bash -x ./menu.sh" -f ../log/session_$now_time.log;;
*)
echo "Invalid option. Quitting"
break ;;
esac
Is there a way to log all +++ output produced by bash -x , but not to display it ?
If I redirect all the output to /dev/null I'm losing the whiptail view as well, however I dont want to lose the bash -x outputs , but just not to be displayed .
Here is what I see when I start my script :
+++ date +%d_%m_%Y
++ now=11_09_2017
+ true
++ whiptail --title 'Multinode Main Menu' --menu '\n\n\n\n\n\n\n\n'...
+ case $OPTION in
+ echo 'Bye !'
Bye !
#
How can I hide all the +++ lines, but log them in the session log file ?
The special variable BASH_XTRACEFD allows to use another file descriptor than 2.
[reference][1]
https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html
exec 5> commands.txt
BASH_XTRACEFD=5
set -x
Or to integrate in provided script
... "BASH_XTRACEFD=5 bash -x ./menu.sh 5> commands.log" ...
also, error output (2>) is redirected in the same file provided by script -f option , is it wanted ?
To have errors and commands of menu.sh in a separate file and not gnu script messages
... "bash -x ./menu.sh 2> err_and_commands.log" ...

Bash - Start a new instance of a command in another terminal seperate from your current terminal

I have a simple bash script (test.sh) set up like this:
#!/bin/bash
args=("$#")
if [[ ( ${args[0]} = "check_capture" ) ]]; then
watch -n 1 'ls -lag /home/user/capture0'
watch -n 1 'ls -lag /home/user/capture1'
watch -n 1 'ls -lag /home/user/capture2'
exit
fi
Files are continuously being written to these target locations capture 0, capture 1, and capture 3. I want to be able to watch these directories using ls command continuously on 3 seperate terminals, and once I run this script (test.sh) from the current terminal, I want it to exit.
Right now it is blocked by each wait, which I know is a blocking bash command waiting for user input control-c. Is there a way I can have the 3 watch commands be executed in seperate terminals then reach the exit statement?
You can start several instances of the terminal in background, each one running a command, like this:
if [[ ... ]]; then
xterm -e 'watch -n 1 "ls -lag /home/user/capture0"' &
xterm -e 'watch -n 1 "ls -lag /home/user/capture1"' &
...
exit
fi
Check man xterm:
-e program [ arguments ... ]
This option specifies the program (and its command line arguments) to be run in the xterm window. It also sets the window title and icon name to be the basename of the program being executed if neither -T nor -n are given on
the command line. This must be the last option on the command line.
The same option works also for xfce-terminal and gnome-terminal.
In addition, xterm (and others) also support setting the title of the window, position, size (called geometry), colors, fonts, and many, many other features.

How to write a shell script to open four terminals and execute a command in each?

So I'm trying to create a shell script to do open up four terminal windows (konsoles preferably) and run a command in each and then keep each of those terminals open so I can continue to execute commands in them if desired.
I tried following the instructions listed here:
How to create a shell script to launch 3 terminals and execute a set of commands in each?
and
How can I make a script that opens terminal windows and executes
commands in them?
and after trying those details the best I have is the following:
#!/bin/bash
# some older test, doesn't work and complains and I get this message on command line: "QApplication::qAppName: Please instantiate the QApplication object first"
# I also can't enter text after command executes
#echo "Hello World!"
#exec konsole --noclose -e cat ~/.aliases
# opens terminal but then I can't control terminal afterwards
xterm -hold -e "echo Hello My World"
# didn't do anything
#exit 0
# didn't do anything except make me type exit an extra time where I executed my shell script
#$SHELL
EDIT:
Using Roberto's answer I get four terminals like this, but I can't enter additional commands, notice how there is no prompt like "mycomputername> ":
EDIT 2:
I found an even better way to do what I want. The script below will execute the commands listed in the cmds array in a separate terminal. So echo 'hello1' will run in one terminal, and echo 'hello2' will run in another terminal. This will continue for as many commands listed in the cmds array
!/bin/bash
# Shell script to open terminals
# and execute a separate command in each
# Commands to run (one per terminal)
cmds=('echo 'hello1'', 'echo 'hello2'')
# Loop through commands, open terminal, execute command
for i in "${cmds[#]}"
do
xterm -e "$i && /bin/tcsh" &
done
Konsole
multiple windows
#!/usr/bin/env bash
konsole --noclose -e echo Hello terminal 1! &
konsole --noclose -e echo Hello terminal 2! &
konsole --noclose -e echo Hello terminal 3! &
konsole --noclose -e echo Hello terminal 4! &
multiple tabs
#!/usr/bin/env bash
konsole --noclose --new-tab -e echo Hello terminal 1! &
konsole --noclose --new-tab -e echo Hello terminal 2! &
konsole --noclose --new-tab -e echo Hello terminal 3! &
konsole --noclose --new-tab -e echo Hello terminal 4! &
You could use a "for" loop, and a "&" to run xterm in background:
#!/bin/bash
# some older test, doesn't work and complains and I get this message on command line: "QApplication::qAppName: Please instantiate the QApplication object first"
# I also can't enter text after command executes
#echo "Hello World!"
#exec konsole --noclose -e cat ~/.aliases
for i in 1 2 3 4
do
# opens terminal but then I can't control terminal afterwards
xterm -hold -e "echo Hello My World" &
done
# didn't do anything
#exit 0
# didn't do anything except make me type exit an extra time where I executed my shell script
#$SHELL
I found this to be quite easily:
#!usr/bin/env bash
echo "Enter the value of n:"
read n
for ((i = 0; i < n; i++ ))
do
xterm -hold -e <enter command> &
# In my case, I used :
# xterm -hold -e sar -P $i 2 5 &
done
And that's pretty much it! Have a good day :)
Note : For those who are newbies, we save this with a file name '.sh'. Also, please note that this will execute n different commands on n different terminals. If you want, you can execute the same command on every terminal, just remove $i from the in do .... done part ;)
On a Linux Mint mate distribution, this will run <commands> in 3 separated terminal windows:
$ cat START.sh
mate-terminal --execute bash -c "<command1>"
mate-terminal --execute bash -c "<command2>"
mate-terminal --execute bash -c "<command3>"
Killing START.sh won't terminate children <commands>.

Start a Pythonscript in a Screen session

i am currently working on a little bash script to start a .py file in a Screen session and could use help.
I have these 2 Files:
test.py (located at /home/developer/Test/):
import os
print("test")
os.system("ping -c 5 www.google.de>>/home/developer/Test/test.log")
test.sh (located at /home/developer/):
#!/bin/bash
Status="NULL"
if ! screen -list | grep -q "foo";
then
Status="not running"
else
Status="running"
fi
echo "Status: $Status"
read -p "Press [Enter] key to start/stop."
if [[ $Status == "running" ]]
then
screen -S foo -p 0 -X quit
echo "Stopped Executing"
elif [[ $Staus == "not running" ]]
then
screen -dmS foo sh
screen -S foo -X python /home/developer/Test/test.py
echo "Created new Instance"
else
exit 1
fi
It works as intendet until it has to start the python script aka. this line:
screen -S foo -X python /home/developer/Test/test.py
when running it in my normal shell i get:
test
sh: 1: cannot create /home/developer/Test/test.log: Permission denied
MY Questions:
I understand the cause of the Permission denied case (works with sudo) but how do i give Permissions and more interestingly, to whom do i give the Permissions to? (python? | screen? | myuser?)
Is the line to create a new instance in which the script runs correct like that?
Can u think of a better way to execute a python script which has to run night and day but is start and stoppable and doesn't block the shell?
To answer your questions:
You should not need to use sudo at all if the proper user/group is set on the scripts.
$ chmod 644 <user> <group> <script name>
The line creating the new instance does not look correct, it should be more like:
screen -S foo -d -m /usr/bin/python /home/Developer/Test/test.py
While using full path to the python exec; remove useless preceding line: screen -dmS foo sh
Screen is more than adequte to prefer such tasks.
Other problems in your script:
Add a shebang to the python script (eg. #!/usr/bin/python)
Typo on line 20 of test.sh: should be $Status, not $Staus
You may need to initially create test.log before executing your script (eg. touch test.log)

Resources