Tuesday, March 18, 2014

HP-UX UNIX: Start / Stop and Configure Cron Services

Cron service is required to run jobs and tasks such as backup.

You must login as the root to run following commands. Each user that is using the cron service must have a cron configuration file in the /var/spool/cron/crontab directory. Also users are permitted if their name appeared in /var/adm/cron/cron.allow file

Task: Find out if cron is running under HP-UX

Type the following command at a shell prompt
# ps -ef | grep cron

Open /etc/rc.config.d/cron file
vi /etc/rc.config.d/cron

Set control variable to 1 to enable cron : CRON=1
Set control variable to 0 to disable : CRON=0

Close and save the file. To start or stop cron you can type the following command:

# /sbin/init.d/cron start <-- start cron

# /sbin/init.d/cron stop <-- stop cron

Task: HP-UX Start cron service

If cron is not running, simply type:
# cron

Task: Edit / create cron jobs

Type the following command to submit a cron job:
# crontab -e

List your cron file:
# crontab -l

Backup all your cron jobs:
# crontab -l > ~/backup.cron.jobs

Remove ALL cron job:
# crontab -r

Task: Crontab file format

Cron file format is as follows:
MIN    HOUR     DATE     MONTH      DAY    /PATH/TO/COMMAND
0-59   0-23      1-31      1-12         0-6     /root/scripts/backup2tape.sh
 
Easy to remember crontab file format:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
 
To run /root/script/backup at 23:00, every day, enter:
# crontab -e

Append following
0   23  * *  * /root/script/backup
 
Run foo job very weekday (MON-Fri) at 6am, enter:
0   6  * *  1-5 /root/script/backup