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.
First make a shell script:
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:
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
service crond restart
To check what are the commands that we write in the crontab can execute the following command:
Done, that was how to make Cronjob on Linux or Linux VPS.