Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart a process every 4 hours using crontab?

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

like image 275
user3079979 Avatar asked Dec 08 '13 14:12

user3079979


People also ask

How do you restart a cron job?

To run a cron job at every system boot, add a string called @reboot to the end of the task list. The job defined by this string runs at startup, immediately after Linux reboots. Note: Always use the full path to the job, script, or command you want to run, starting from the root.

Does cron require restart?

No. As long as you use the crontab -e command to edit the file, when you save it, you'll get a 'New Crontab Installed' message. That's it.

Does cron start automatically?

Cron jobs are tasks that run automatically following some predefined schedule.

Do I need to restart cron after changing crontab?

1 Answer. No. Come and join Linux training to gain great knowledge.


2 Answers

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.

like image 59
m4gix1 Avatar answered Oct 27 '22 14:10

m4gix1


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)

like image 27
mcleod_ideafix Avatar answered Oct 27 '22 12:10

mcleod_ideafix