Linux change to another /dev/ttyX and run program there - linux

Is it possible to run a script on /dev/tty1 and spawn a program to run on /dev/tty2 and wait for it to complete and run the other commands in the script? For instance:
echo "Hello, this is from terminal 1"
chvt 2
sh myprogram.sh (I want it to run on tty2, but it runs on tty1)
chvt 1
myprogram.sh:
echo "Hello' this is from terminal 2, please type your input:"
read A
exit A

Yes, you can do this using openvt.
openvt -c 2 sh myprogram.sh

Related

ssh does not return even after execution

The following ssh command does not return to terminal. It hangs though the execution is completed. The execution hangs after echo hi command.
ssh user#testserver "echo hello;source .profile;source .bash_profile;/apps/myapp/deploytools/ciInstallAndRun.sh; echo hi"
Output
hello
<outoutfrom remote script"
hi
ciInstallAndRun.sh
echo 'starting'
cd /apps/myapp/current
./tctl kill
cd /apps/myapp
mv myapp_v1.0 "myapp_v1.0_`date '+%Y%m%d%H%M'`"
unzip -o /apps/myapp/myappdist-bin.zip
java -classpath .:/apps/myapp/deploytools/cleanup.jar se.telenor.project.cleanup.Cleanup /apps/myapp myapp_v1.0_ 3
cd /apps/myapp/myapp_v1.0
echo 'Done with deploy'
chmod -R 775 *
echo 'Done'
./tctl start test
Source OS: Redhat
Dest Os: Solaris 10 8/07
Any idea to fix this.
Any idea to fix this.
Your installation script has spawned a child process.
Add a ps -f or ptree $$ command before echo hi. You'll see a child process or multiple child processes spawned by your install script.
To stop the SSH command from hanging, you need to detach such child process(es) from your terminal's input/output. You can sedirect your script's output to a file - both stdout and stderr with > /some/output/file 2>&1, and also redirect its input with < /dev/null.
Or you can use the nohup command.
You haven't provided an MCVE, as others have noted, but this is likely the problem command in you install script, since your question implies that you see the expected output from your install script:
./tctl start test
You probably would do better to replace it with something like:
./tctl start test </dev/null >/some/log/file/path.log 2>&1

Open gnome-terminal from shell script and hold window open

I'm trying to write a script that sets up my programming environment by taking as an argument the name of the project(directory) I want to open
so for example: ./my_script.sh sample_app
And here is the script so far:
#!/bin/bash
wmctrl -s 0
sublime &
sleep 2
wmctrl -s 1
google-chrome &
sleep 2
wmctrl -s 2
Dir="$echo /home/biTNumb/Projects/ruby/workspace/$1"
echo $Dir
gnome-terminal --window-with-profile=Guard -e ls &
#gnome-terminal -e cd /home/biTNumb/Projects/ruby/workspace/$1 && bundle exec guard &
#gnome-terminal -e cd /home/biTNumb/Projects/ruby/workspace/$1 && rails s &
The problem arises when the script executes:
gnome-terminal --window-with-profile=Guard -e ls &
I get the message The child process exited normally with status 0.:
I can't use the terminal after (I can only close it) and the message hides some part of the output. And yet, I have already created a profile and set the When command exits dropdown menu to 'Hold the terminal open'...
** I left the comments in the script so you can get the idea of what I'm trying to do
To execute multiple commands in the same gnome-terminal window, use semicolons ; as separators like below:
gnome-terminal -- bash -c "command 1; command 2; ...; bash;"
The last command bash starts the command-line interpreter so that the terminal doesn't exit at the end (use $shell instead of bash for a more general solution).
This way you won't need to define a profile with the option Hold the terminal open when command exits and you won't get the message The child process exited with status 0 (which hide some parts of the output).
Here is what you might write (I use backslashes \ to split lines):
gnome-terminal -- bash -c \
" ls ;\
cd /home/biTNumb/Projects/ruby/workspace/$1 && bundle exec guard ;\
cd /home/biTNumb/Projects/ruby/workspace/$1 && rails s;\
$bash;"

How can we open minicom from current terminal and pass multiple commands to execute and exit to 1st terminal using shell script

I am trying to write a shell script that creates a new window and run a minicom terminal (connected to /dev/ttyACM0) in that.
Here's the script file my_script.sh:
#!/bin/bash
gnome-terminal --command minicom
echo "\n" >> /dev/ttyACM0
sleep 5
echo "\n" >> /dev/ttyACM0
echo "run x_boot" >> /dev/ttyACM0
sleep 5
echo "root" >> /dev/ttyACM0
sleep 3
echo "cd /tmp" >> /dev/ttyACM0
sleep 1
In the above code all the echo commands i'm passing directly to the device file not to the minicom terminal.
Requirements:
Now I need to send command1 to minicom
Make the terminal to sleep for 5 sec before sending next command
Send Command2
Wait again for 5sec.
And many commands automated
After that exit the terminal without closing minicom
Please help me with this.
Use minicom scripting (runscript) instead of bash echoes. It has both send and sleep commands:
-S, --script=SCRIPT : run SCRIPT at startup

How to get the process id of command executed in bash script?

I have a script i want to run 2 programs at the same time, One is a c program and the other is cpulimit, I want to start the C program in the background first with "&" and then get the PID of the C program and hand it to cpulimit which will also run in the background with "&".
I tried doing this below and it just starts the first program and never starts cpulimit.
Also i am running this as a startup script as root using systemd in arch linux.
#!/bin/bash
/myprogram &
PID=$!
cpulimit -z -p $PID -l 75 &
exit 0
I think i have this solved now, According to this here: link I need to wrap the commands like this (command) to create a sub shell.
#!/bin/bash
(mygprgram &)
mypid=$!
(cpulimit -z -p $mypid -l 75 &)
exit 0
I just found this while googling and wanted to add something.
While your solution seems to be working (see comments about subshells), in this case you don't need to get the pid at all. Just run the command like this:
cpulimit -z -l 75 myprogram &

Run a another within script

I have to run another terminal within script and wait for function to execute on that terminal
before proceeding next command in shell script. Scenario is as like below
script.sh
!/bin/sh
...
...
gnome-terminal --working-directory=#{image_path} -e ./test.sh # execute test.sh script on another terminal
./switch 0 0 # I have to execute this command after test.sh script gets completed on another terminal
...
...
Here problem is. test.sh script is not gets executed fully and returned back immediately to parent script and executes ./switch 0 0 command. As this ./switch 0 0 command depends on test.sh script completion, my script gets failed.
Regards
Manish B.
I'm not sure if there's a switch in gnome-terminal that can make it run synchronously, but you can use xterm for that. Xterm won't go to the background unlike other terminals:
( D=$(pwd); cd "$image_path"; xterm -e "$D/test.sh" )
./switch 0 0

Resources