Linux – Starting script in screen with cron

bashcrongnu-screenlinux

I apologise if this has been asked before but I am not sure how to question a search query for this.
I am extremely new to linux and I have been learning as I go to solve issues.

I am trying to set up working cron jobs to restart a game server I am running.

Currently my crontab looks like this

#backup world as instructed by https://github.com/g1franc/SEDS-Setup
0 0 * * * /home/root/spaceengineers/start.sh backupworld
#close server
1 0 * * * /usr/bin/screen -S spaceengineers -X stuff "^C"
#close screen
2 0 * * * /usr/bin/screen -S spaceengineers -X stuff "^M"
#recreate server
3 0 * * * /home/root/spaceengineers/start.sh

Now on the backup world I get in my syslog file located at /var/log i get

May 24 00:00:01 SpaceEngineers CRON[1958]: (root) CMD (/home/root/spaceengineers/start.sh backupworld)
May 24 00:00:01 SpaceEngineers CRON[1957]: (CRON) info (No MTA installed, discarding output)

and that is also the same with the recreate server

May 24 00:03:01 SpaceEngineers CRON[2269]: (root) CMD (/home/root/spaceengineers/start.sh /usr/bin/screen -x spaceengineers)
May 24 00:03:01 SpaceEngineers CRON[2268]: (CRON) info (No MTA installed, discarding output)

The other commands work perfectly fine.

now before anyone questions why I have structured my code the way I have, its because the tutorial i was following says to start the server via ~/spaceengineers/start.sh -x spaceengineers
and from what I have read i need to do /usr/bin/screen to access any screen commands in Cron

What am I doing wrong and what do I need to do to rectify my issues.

EDITED: Changed the crontab to match current contents on my server, issues remain the same

Pastebin of start.sh

http://pastebin.com/9QcWyqYF

Best Answer

Can you check the GNU screen syntax for this line:

3 0 * * * /home/root/spaceengineers/start.sh /usr/bin/screen -x spaceengineers

I would've expected something like:

3 0 * * * /usr/bin/screen -S spaceengineers -X stuff 'command with newline' 

However, using the -X means screen expects screen commands and not a script to run.

Therefore you need to use the screen stuff command as per this SU question

screen -S sessionname -X stuff 'command'`echo -ne '\015'`

Update

Try /bin/bash or wherever the Shell you use is located.

 3 0 * * * /bin/bash /home/root/spaceengineers/start.sh

Maybe add contents of start.sh to help debug.

You can find shell with the which command. Might have to sudo or be root to find out.

Related Question