8

Could someone tell me how to restart a process every 4 hours using crontab? I have a Starbound server running (which is a game like Terarria which recently came out) and it's taking a lot of resources, so I'd like to kill the process then start it back up every 6 hours.

What I think I would need to do in crontab is:

kill -9 | grep starbound_server cd /home/steam/starbound/linux64 && screen -S starbound -d -m ./launch_starbound_server.sh

But I am not sure about this and don't understand the time thingy either.

I hope someone can help me :)

user3079979
  • 105
  • 1
  • 1
  • 7
  • 1
    Possible duplicate: http://stackoverflow.com/questions/11562804/running-cron-job-on-linux-every-6-hours – aste123 Dec 08 '13 at 14:23

2 Answers2

27

crontab works like this.

# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

So if you want to run your script every minute on 4 hour intervals, you'd have to add this line to crontab file.

* */4 * * * user-name command to be executed

To run your script once every 4 hours (on the zero minute), you'd have to add this line to crontab file.

0 */4 * * * user-name command to be executed

Edit ( Answer to comment ):

Yes, I believe this is correct, but as myself I usually do separate file for this, for example, script.sh to keep things clean.

For example with contents:

#!/bin/sh

# Kill 1
screen -X -S | grep starbound kill 

# Kill 2
kill -9 | grep starbound_server

# Change directory  
cd /home/steam/starbound/linux64

# Start the server again 
screen -S starbound -d -m ./launch_starbound_server.sh

You can save it to the location you like and use:

chmod +x yourcript.sh

to make it executable, and then add it to crontab.

JoeG
  • 7,191
  • 10
  • 60
  • 105
m4gix1
  • 346
  • 4
  • 8
  • Wow thanks, I actually can't believe how well you explained that! Wish I could upvote you. If you don't mind me asking another question, would this be correct: * */4 * * * my_user_name screen -X -S | grep starbound kill && kill -9 | grep starbound_server && cd /home/steam/starbound/linux64 && screen -S starbound -d -m ./launch_starbound_server.sh – user3079979 Dec 08 '13 at 14:34
  • Hey awesome, you're my savior! But screen -X -S | grep starbound kill doesn't seem to work, I've googled but can't find anything how to search/find a screen then kill it. I want to search it because sometimes it doesn't want to get killed and I have to put pid.screen. – user3079979 Dec 08 '13 at 15:03
  • 3
    Beware that the first * will run the script every minute during the 4th hour. I hould HIGHLY suggest setting it to 0 to have it only run once every 4 hours. I.E. 0 */4 * * * – thiesdiggity Feb 15 '14 at 18:18
  • I made an edit so now it is correctly written `0 */4 * * *` not `* */4 * * *` – valentt Nov 26 '14 at 15:18
  • just wondering - do we really need screen here, though? I mean, cron will keep background processes alive anyway, right? – Philipp_Kats Jan 06 '18 at 16:01
1

Provided that you have installed the starbound server start script in /etc/init.d

http://www.bubblews.com/news/1749423-starbound-server-start-script

And you named it starbound.sh

Then, add a line in your /etc/crontab like this:

0 /4 * * * root /etc/init.d/starbound.sh restart

(NOTE: this is in case the starbound server is started by root: check that the server itself drops its priviledges upon starting if it doesn't need them)

mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
  • 2
    It should be `0 /4 * * * root /etc/init.d/starbound.sh restart` to execute once every hour. Not every minute of every 4th hour. I think it's clearly what OP wants. – naktinis May 12 '14 at 16:32