1

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

TheAngryBr1t
  • 11
  • 1
  • 3
  • 1
    The message about MTA is happening because CRON tries to send email on job completion. It can be safely ignored, unless you want the job output to be sent via email. Are the two problematic jobs actually running as expected? Do they do what you want? – boot13 May 23 '15 at 17:33
  • @boot13 Just rechecked and the backup script seems to be doing something now but the recreate server job is not doing anything – TheAngryBr1t May 23 '15 at 17:40
  • Can you add the relevant bits from the start scriot – fswings May 24 '15 at 00:24

3 Answers3

0

This command worked for me, though because one or more of the commands in my script required sudo, I had to set this up under the root user's cron :

17 2 29 1 * /usr/bin/screen -dmS $MYSESSIONNAME /bin/bash $FULLPATHTOMYSHELLSCRIPT $SHELLSCRIPTARG1 $SHELLSCRIPTARG2 ...

The key to getting screen working (and not just closing immediately when invoked by cron) is the -d argument, but the m probably helps too in some way/cases.

Below is the relevant snippet from the screen --help output

-dmS name Start as daemon: Screen session in detached mode."

Joseph
  • 1,341
  • 2
  • 16
  • 33
Jack Hadley
  • 61
  • 1
  • 3
0

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.

fswings
  • 833
  • 1
  • 11
  • 27
  • I have just removed the `usr/bin/screen` stuff as it is no longer needed. turns out the script is creating a screen by itself, I believe that to be when running a program through WINE. as I said I am learning on the fly. But with this stuff now removed the script is still not executing. the recreate server line now reads: `3 0 * * * /home/root/spaceengineers/start.sh` The reason I have changed it to that is the backup script is working and its called the same way. have edited the question with the new crontab – TheAngryBr1t May 23 '15 at 23:18
  • In my crontab had to specify the Shell location. – fswings May 23 '15 at 23:41
  • I added /bin/bash after checking what shell I am using and making sure i had my Crontabs SHELL path set to `SHELL=/bin/bash` I am still not getting anywhere. I have made sure the shebang in the script is set correctly as well. **UPDATE:** Finally seeing in the syslog that it is trying to run commands. For the last 2 hours I wasnt getting anything in the log. `May 24 00:03:01 SpaceEngineers CRON[2594]: (root) CMD (/bin/bash /home/root/spaceengineers/start.sh)` It still is not actually creating the screen like it normally does if run manually, but i guess its a step forward. – TheAngryBr1t May 24 '15 at 00:22
  • The script may have to be modified so it uses absolute paths. – fswings May 24 '15 at 00:23
  • http://pastebin.com/9QcWyqYF here is pastebin of code – TheAngryBr1t May 24 '15 at 00:26
  • The script takes one of three arguments `start`, `setup` and `backupworld`. It also tries and works out its own home directory. As root when you type in `cd` and then `pwd` do you end up in `/home/root`? – fswings May 24 '15 at 08:44
  • I typed in `pwd` and it returned `/root`. But if cron was to execute that command would that return a different directory? I also added the start argument to the end of the cron tab. Didn't realise I needed to use that as I dont use it when I start it via comand line – TheAngryBr1t May 24 '15 at 09:05
  • I'm trying to address that very point. Which user runs cron. In my setup it's root BUT root doesn't have a home directory or it isn't /home/root. So as a test replace any $HOME instance in script with full path eg /home/root. Alternatively set the environmental variable before running script. – fswings May 24 '15 at 10:04
  • Along side that should i replace any `whoami` statements as well? – TheAngryBr1t May 24 '15 at 10:28
  • Yes! Change the script so that it reflects the same user and home directory that the script ran successfully with. – fswings May 24 '15 at 10:33
  • When you run this script directly (without cron) do you do it as root or some other user? – fswings May 24 '15 at 10:34
  • I run it as root. I know that its better to create different users instead but I didn't.. and it takes about an hour or 2 on my internet to reupload everything if i start again – TheAngryBr1t May 24 '15 at 10:42
  • So I changed all the paths to go direct to the directories using `/home/root` and replaced the `whoami` statements as well with `root` and no success. – TheAngryBr1t May 24 '15 at 10:59
  • I can't think what else might be at cause. Could you post the updated script? As my last act. – fswings May 24 '15 at 11:14
  • http://pastebin.com/UW3DDKLb even if I can't solve this with your help after this, I thank you for your efforts, I have actually learnt quite a bit just from doing this :) – TheAngryBr1t May 24 '15 at 11:17
  • Sorry, looks like this is beyond me. Hopefully someone who understand cron better can help. One thing I Learned is that cron knows nothing about the environment you're running in. Which is the principle I tried to practice but unfortunately hasn't worked now. – fswings May 24 '15 at 11:24
  • He should try sending the screen `kill` command (not to be confused with the `kill` command for killing a process) to the screen because it will issue `SIGHUP`, making the process exit, then closing the screen. It also eliminates the need for the third cronjob to kill the screen. – Joseph Mar 03 '19 at 06:15
-1

@daily /usr/bin/screen -dms aquaticscenery /ig.py && python ig.py