Create cron jobs on Linux VPS

0
2336

Sometimes we need to execute a specific command on the specified time span for example clears the cache etc. We can use a crontab to do so.

Cronjob

First make a shell script:

vi clearcache.sh

Then write the following script:

#!/bin/sh
free && sync && echo 3 > /proc/sys/vm/drop_caches && echo "" && free

Save the script by pressing the Shift + ZZ.

Make sure if the script we created earlier can be executed by doing command:

chmod +x clearcache.sh

Now we create the crontab:

crontab -e

Here is a guide to the time when the cron will run:

# Example of job definition:
# .---------------- 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

For example, we will be running the script we already create a yesteryear once every 2 hours then fill in the crontab:

0 */2 * * * /root/clearcache.sh

Then restart:

service crond restart

To check what are the commands that we write in the crontab can execute the following command:

crontab -l

Done, that was how to make Cronjob on Linux or Linux VPS.

NO COMMENTS

LEAVE A REPLY

This site uses Akismet to reduce spam. Learn how your comment data is processed.