Question

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 :)

Was it helpful?

Solution

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.

OTHER TIPS

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top