How to close a screen from a Makefile? - bash

I have a test that depend on a specific HTTP server, which requires me to start one with a known setup for the tests.
Since the server cannot be started as a daemon my approach was to just have it start in a screen session, run the test and close the session.
test:
screen -S test_http_server -d -m start_my_test_http_server
# run my tests here
screen -S test_http_server -X kill # works from bash but not makefile :/
Everything works fine except for closing or killing the session (which does work if I run it in bash afterwards).

It seems that using the # prefix (which I did, but was not posted in the original example code) that suppresses the normal 'echo' of the command somehow interferes with closing the screen.
Fails because of # prefix usage.
test:
#screen -S test_http_server -d -m start_my_test_http_server
# run my tests here
#screen -S test_http_server -X kill
Fixed make file that works as intended.
test:
screen -S test_http_server -d -m start_my_test_http_server
# run my tests here
screen -S test_http_server -X kill

Related

Terminal script involving 2 terminals

Is it possible to make a command that does something, Opens another terminal and then does something there?
Im trying to combine the start up of a server and its api.
"god": "next dev && cd server && strapi develop"
I want the 'cd' part to start in a new terminal. Ive tried open -a terminal but it doesnt seem to work.
you can try to use tmux for that
tmux new-session \; \
send-keys 'next dev' C-m \; \
split-window -h -p 50 \; \
send-keys 'cd server && strapi develop' C-m \;
If I understand the question correctly, you simply need to run two commands in parallel. This can be achieved easily in bash:
command1 & command2 # will run command1, send it to background,
# and run command2 (which will be kept in foreground)
In your case:
next dev & cd server && strapi develop
You can read more about this here: How do you run multiple programs in parallel from a bash script?

Not able to send psadmin output to logs

I'm making a script to restart an instance and it works without any log file but it gives the following error when I try to log the output of psadmin:
java.lang.NullPointerException
at com.peoplesoft.pt.psadmin.ui.Progress.<init>(Progress.java:135)
at com.peoplesoft.pt.psadmin.ui.Progress.getInstance(Progress.java:123)
at com.peoplesoft.pt.psadmin.pia.DomainBootHandler.BootWlsServer(DomainBootHandler.java:84)
at com.peoplesoft.pt.psadmin.pia.DomainBootHandler.run(DomainBootHandler.java:62)
at com.peoplesoft.pt.psadmin.pia.PIAAdminCmdLine.startDomain(PIAAdminCmdLine.java:270)
at com.peoplesoft.pt.psadmin.pia.PIAAdminCmdLine.run(PIAAdminCmdLine.java:481)
at com.peoplesoft.pt.psadmin.PSAdmin.runSwitched(PSAdmin.java:170)
at com.peoplesoft.pt.psadmin.PSAdmin.main(PSAdmin.java:232)
The following works (with no log):
export ORAENV_ASK=NO
export ORACLE_SID=PSCNV
.oraenv
export TUXDIR=/m001/Oracle/Middleware/tuxedo12.1.1.0
. /m001/pt854/psconfig.sh
. $TUXDIR/tux.env
export PS_CFG_HOME=$PS_HOME
$PS_HOME/appserv/psadmin -w shutdown -d PSCNV
$PS_HOME/appserv/psadmin -w start -d PSCNV
$PS_HOME/appserv/psadmin -w status -d PSCNV
Changing the psadmin invocations like so causes the error:
LOGFILE=/home/psoft/scripts/pscnv_webserv_stopNstart.log
test() {
$PS_HOME/appserv/psadmin -w shutdown -d PSCNV
$PS_HOME/appserv/psadmin -w start -d PSCNV
$PS_HOME/appserv/psadmin -w status -d PSCNV
}
test >> ${LOGFILE}
I also tried redirecting the output of each call individually and saw the same error.
I'm interested in any feedback to this question as well. I tried writing a cross platform java program to bounce multiple app and web servers and it seems that the psadmin.jar program exclusively holds onto stdout during the psadmin program.
I want to evaluate the output of psadmin/psadmin.jar to see if there are trappable errors that require killing of the process at the os level.
Hopefully there is a way to share stdout, but I have not found a way yet...
This solved this for me. nohup script -q -c "psadmin -w start -d peoplesoft"

bash scripting tmux, multiple detach/attach

I have a process I want to monitor by tail -f on several output files in different directories. I can use a bash script start tmux as a detached session, create multiple panes, change to the top directory and reattach. This all works. My problem comes when I want the script to send more commands later. Is there some reason why once I attach, my script can't send commands or detach/reattach later? The reason to do more commands is that some files take 45 seconds to be created before I can tail them.
My example looks like
#!/bin/bash
# this depends on some settings from my ~/.tmux.conf
TopLevel='/tsload'
SimDir=`ls -d $TopLevel/SIM_ISS*`
# create and detach session
tmux new-session -s simwatch -n Sim_Watch -d
# make left & right panes, only 1 window
tmux split-window -h -t simwatch
# change to toplevel dir
tmux send-keys -t simwatch:1.1 "cd $SimDir" C-m
tmux send-keys -t simwatch:1.2 "cd $SimDir" C-m
tmux attach -t simwatch
At this point my script fails when I try to have it do more. I've also tried 'tmux detach -t simwatch' issue commands and reattach but they don't take effect.
Not very familiar with tmux, but from my experiment with your script, it looks like that tmux 'attach -t simwatch' is a blocking operation, i.e. it starts and will end once you detach/exit from the session, which is when the script will continue.
Regarding the issue with the files that appear later, if you know their paths, you can try to follow them with "tail -F", which will wait for the files to appear.
tail --follow=name --retry missing_file

How can I start a screen session using a specific config file?

I would like to be able to start a screen session using a certain config file. I know that I can use -c and then the file path to the config file but if I do that, then the sh script I am using does not work. You can see the sh script below:
#!/bin/bash
cd /media/kiancross/Minecraft_Server/1.6.4
screen -d -m -S MinecraftServer ./start.sh
screen -r MinecraftServer
I would have thought that I can do the following code:
#!/bin/bash
cd /media/kiancross/Minecraft_Server/1.6.4
screen -d -m -S -c MinecraftServer $HOME/config_file/mcserver.config ./start.sh
screen -r MinecraftServer
But then I get a message saying:
There is no screen to be resumed matching MinecraftServer.
After then checking to see if there is a screen session running it says that there are no screen sessions running
No Sockets found in /var/run/screen/S-kiancross.
Does anybody know how I can do this so that I can use a custom config file?
The command should be:
screen -d -m -S MinecraftServer -c $HOME/config_file/mcserver.config ./start.sh
The name of the screen session goes after -S and the path of the config file goes after -c. You inserted -c before the screen name.

open gnome terminal tabs programmatically and execute commands in sequence

When working remotely, I have a series of tabs that I open in gnome-terminal, and commands that I execute in them. I would like to automate all this setup as a single command.
If these commands could run independently and in parallel, I'd just adapt the answer to this question. In fact, I tried, using the following shell script:
gnome-terminal --working-directory="/home/superelectric" --tab -t "gate" -e 'bash -c "export BASH_POST_RC=\"ssh gate_tunnel\"; exec bash"' --tab -t "mydesktop" -e 'bash -c "export BASH_POST_RC=\"ssh tunneled_mydesktop\"; exec bash"'
Spread out over multiple lines, for readability:
gnome-terminal \
--working-directory="/home/superelectric" \
--tab \
-t "gate" \
-e \
'bash -c "export BASH_POST_RC=\"ssh gate_tunnel\"; exec bash"' \
--tab \
-t "mydesktop" \
-e \
'bash -c "export BASH_POST_RC=\"ssh tunneled_mydesktop\"; exec bash"'
The first part opens a tab, names it 'gate', and executes 'ssh gate_tunnel' within it. This is an ssh alias that opens a tunnel to 'mydesktop' at school, through the school's outward-facing server, 'gate'.
The second part opens another tab, names it 'mydesktop', and executes 'ssh tunneled_mydesktop' within it. This is another ssh alias, which connects to mydesktop through the tunnel.
~/.ssh/config:
Host gate_tunnel
LocalForward 8023 <my_desktop_at_school>:22
HostName <my_school_server>
That's the theory. In practice, the two commands execute in parallel, whereas I need to ensure that the first tab's command (open tunnel) completes before executing the second tab's command (connect through tunnel).
Is there maybe some command I can execute in the second tab, that 'waits' until the ssh tunnel is opened?
Ok, I think i get it. As i mentioned in the comments the first thing that comes to mind for reaching your school desktop from the outside is to ssh into the school gate and from there ssh into your desktop with something like:
$ ssh -t gate.school.edu ssh desktop_name
There's only one tab then, so your problem doesn't exist.
However there's something very cool with your current setup:
From home it's almost as if you had a direct connection to your desktop machine, so you can scp into it directly and forget about gate. With the solution above that's not possible anymore because we end up with an indirect connection: If you want to scp you have to do it from gate and that sucks.
Check out this article on using ssh's ProxyCommand feature:
Transparent Multi-hop SSH
You get the best of both worlds then :)
Hmm... this may not be a perfect solution. Ideally you should use something that monitors the ssh connection. But, you can check the ssh process with ps. And wait for ssh command to come alive.
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do # try 10 times
if ps aux ¦ grep <my_desktop_at_school> then
# the tunnel connected now execute the second command
'bash -c "export BASH_POST_RC=\"ssh tunneled_mydesktop\"; exec bash"'
else
continue # or you could do something here if you wish
fi
sleep 10 # sleep for 10 seconds and try again
let COUNTER=COUNTER+1
done
You will have to run this script in the second tab.
Hope it helps.

Resources