0

I have 2 scripts that I'm testing to automate starting services on my server however they behave weirdly.

The first script is

#!/bin/sh
screen -dmS Test_Screen
sleep 1
sudo sh cd.sh
echo "finished"

Which runs perfectly however the script it runs does not and is as follows

#!/bin/sh
screen -S Test_Screen -X stuff "cd /home/Test"
sleep 1
screen -S Test_Screen -X eval "stuff \015"
sleep 1
echo "Complete"

The second script will run perfect if I run it from command line and will CD into the directory within the screen. However, if it runs from the first script it Will Not CD into the correct directory within the screen, but it will still print "Complete".

I'm Using CENTOS 6.7 and the latest version of GNU screen

Any Ideas?

JLennon
  • 1
  • 1

2 Answers2

0

This seems to be a problem with session nesting. In your first script you create a session named Test_Screen. In your second script the -S parameter tells screen to create a session of the same name. This might cause screen to exit and not cd into the correct directory.

You could move the cd command in front of the sudo sh cd.sh and remove those screen calls from the second script leaving only

stuff \015
echo "Complete"

Using the correct screenflags should also work.

#!/bin/sh
screen -dr Test_Screen -X stuff "cd /home/Test"
sleep 1
screen -dr Test_Screen -X eval "stuff \015"
sleep 1
echo "Complete"

For a more modern alternative to screen, have a look at tmux.

cb0
  • 8,415
  • 9
  • 52
  • 80
  • Thanks for the info, turns out the issue was actually Centos 6.7 itself, we update to centos 7 and the script ran perfectly. I'm not sure what happened but the main thing is it works – JLennon May 08 '16 at 23:34
0

Ok so this turned out really weird. After posting i tried a couple of things on a centos 6.7 hyper V test environment and got the exact same issue. However, later in the day we ended up changing service provider and upgrading to centos 7 in the process. I am not sure why but since the update the script now runs perfectly and i was able to actually merge the two scripts into one in order to make it more efficient. If anyone knows why the update fixed it feel free to let me know.

JLennon
  • 1
  • 1