There are cases wherein we have to execute a command or a script say for daily basis for some maintenance purposes. In Linux there is a way that we could do it. It is through crontab (a cron job).
Assuming that we want to execute a command. Say we want to reset and resync our clock time with NTP server pool.
a.) Set up for scheduling command
The first thing to do is to locate the complete path of the command. So for our example let us locate where the complete path for ntpdate command is. Issue this command:
# whereis ntpdate
This will show you the different paths like this:
ntpdate: /usr/sbin/ntpdate /usr/share/man/man8/ntpdate.8.gz
Just take note of the bin or sbin path of the command. So in our case it is /usr/sbin/ntpdate.
b.) for scripts here's the set up
First create your script. If it is a shell script then on the first line of code you must have this. For other scripts like PHP just google out there and surely you will find many.
#!/bin/sh
Save your script. Set the file permission to 775 by issuing:
# chmod 775 (script)
Take note of the path where you saved the script we need this later on.
Next we'll open the crontab file by issuing this command.
# crontab -e
The flag 'e' signifies editing of the crontab file.
Once you're prompted on a file like this
~
~
~
~
"/tmp/crontab.XXXXtkRgpi" 7L, 546C
Press 'i' or insert key then you can start writing your schedules. When you are done. Just press escape key then press colon then 'w' and 'q' (:wq (a vi editor way of saving remember..)).
Before we proceed let me just discuss the format of the syntax that you will be enterring here.
* * * * * /complete/path/to/script/(command / filename of script to execute)
Example
*/1 * * * * /usr/sbin/ntpdate north-america.pool.ntp.org > /dev/null 2>&1
Let me explain...
the first * denotes minute (it can be from 0 - 59) minute
the second * denotes hour (military time 0 - 23) hour
the third is day of month (1 - 31)
4th is month (1 - 12)
5th the last one is day of week (0 - 6) 0 = Sunday
Say you want a schedule everyday at 12 midnight... here is an example
0 0 * * * /complete/path/to/script/(command / filename of script to execute)
If you want to execute per minute, here is how
*/1 * * * * /usr/sbin/ntpdate north-america.pool.ntp.org > /dev/null 2>&1
To list your current active cron jobs, issue:
# crontab -l
I hope you find these stuffs useful. Thanks, Cheers and God Bless!!!
No comments:
Post a Comment