dropdown menu

AIX - CRONTAB

CRONTAB:

The cron daemon, which translates to Chronological Data Event Monitor, is a program that schedules jobs to run automatically at a specific time and date. The /etc/inittab file contains all the AIX startup programs, including the cron daemon. The init process in AIX starts the cron daemon, or cron, from the inittab file during the initialization process of the operating system.

You can submit jobs, or events, to cron by doing one of the following:
Use the at and batch facilities to submit jobs for one-time execution.
Use the crontab files to execute jobs at regularly scheduled intervals (hourly, daily, weekly, and so on).

By default, cron can concurrently run 100 events of equal importance. The /usr/adm/cron/queuedefs file allows you to change this schedule.

c.200j10n120w
| |    |   |
| |    |   wait period (in seconds)
| |    nice value
| jobs
cron

At regularly scheduled intervals, cron looks for and reads the crontab files that are located in the directory /var/spool/cron/crontabs.

These files contain jobs submitted by users. For example, the file /var/spool/cron/crontabs/john contains John's jobs that are scheduled to be run by cron. The files in this directory are named for the individual users. When changes are made to the files, the cron daemon must be notified to reread the files. If you open crontab with "ccrontab -e" and after quit, crontab daemon will be notified an re-reads that file.

/var/spool/cron/crontabs   users`s crontab files are located here
/var/adm/cron/log          cron daemon creates a log of its activities
/var/adm/cron/cron.deny    Any user can use cron except those listed in this file
/var/adm/cron/cron.allow   Only users listed in this file can use cron (root user included)

crontab -l                 Lists the contents of your current crontab file
crontab -e                 Edits your current crontab file (when the file saved, the cron daemon is automatically refreshed.)
crontab -r                 Removes your crontab file from the crontab directory
crontab -v                 check crontab submission time
crontab mycronfile         submit your crontab file to /var/spool/cron/crontabs directory

crontab file format:
minute    hour    day_of_month    month        weekday        command
0-59      0-23    1-31            1-12        0-6 Sun-Sat     shell command

* * * * * /bin/script.sh        schedule a job to run every minute
0 1 15 * * /fullbackup          1 am on the 15th of every month
0 0 * * 1-5 /usr/sbin/backup    start the backup command at midnight, Mo - Fr
0,15,30,45 6-17 * * 1-5 /home/script1                                     execute script1 every 15 minutes between 6AM and 5PM, Mo - Fr
0 1 1 * * /tmp -name 'TRACE*' -mtime +270 -exec rm {} \\; >/dev/null 2>&1 it will delete files older than 9 months
                                                                          (\\; <-- double "\" needed because to interpret ";" correctly)
----------------------------

Ctontab: Adding and Removing lines with script:

To add a line to cron (in this example "0 1 * * * /tmp/test.sh >> /tmp/test.log")
crontab -l | awk '{print} END {print "0 1 * * * /tmp/test.sh >> /tmp/test.log"}' | crontab

To remove an line from cron (in this example any lines that match "/tmp/test.sh >> /tmp/test.log")
crontab -l | sed '\!/tmp/test.sh >> /tmp/test.log!d' | crontab

----------------------------

AT:

at                          submits a job for cron to run at a specific time in the future
                            (at -f /home/root/bb_at -t 2007122503)
echo "<command>" | at now   this starts in the background (and you can log off)

at now +2 mins
banner hello > /dev/pts/0
<ctrl-d>                    (at now + 1 minute,at 5 pm Friday )


/var/adm/cron/at.deny       allows any users except those listed in this file to use the at command.
/var/adm/cron/at.allow      allows only those users listed in this file to use the at command (including root).
at -l                       Lists at jobs
atq [user]                  Views other user's jobs (Only root can use this command.)
at -r                       Cancels an at job
atrm job                    Cancels an at job by job number
atrm user                   Cancels an at job by the user (root can use it for any user; users can cancel their jobs.)
atrm                        Cancels all at jobs belonging to the user invoking the atrm command

batch                       submits a job to be run in the background when the processor load is low

41 comments:

Anonymous said...

Thank you for the explanation. But I want to know the command to follow the running trace when I lanch at command. I tried "mail" before it returns messages in the spool, but now i receive nothing. can you help me?????

aix said...

You can try /var/adm/cron/log, because as I checked at jobs will be logged there as well.

And you can set the standard out and err, when you run your at job.

For example:
root@bb_lpar: /tmp/bb # echo "find / -name "*err*" > /tmp/bb/find.out 2>/tmp/bb/find.err" | at now +2 mins
Job root.1319448878.a will be run at Mon Oct 24 11:34:38 CEST 2011.

root@bb_lpar: /tmp/bb # at -l
root.1319448878.a Mon Oct 24 11:34:38 CEST 2011

root@bb_lpar: /var/adm/cron # tail -f log
root : CMD ( root.1319448878.a ) : PID ( 16908368 ) : Mon Oct 24 11:34:38 2011
Cron Job with pid: 16908368 Failed

It shows as a "Cron Job", but as you saw it was an At Job.

Anonymous said...

Thanks for shaing the article. You can find more about crontab at crontab command examples in linux

aix said...

Thanks for this good info. I found very useful data there, and I will update this page soon with it :)

Anonymous said...

Hi,
I am facing the below messages with cron very frequently.

Do you know how should i rectify this ?

! cannot access /var/adm/cron/FIFO queue Mon Mar 17 15:36:24 2012
! ******* CRON ABORTED ******** Mon mar 17 15:36:24 2012

aix said...

I never faced with this problem, so after Google it I have found this:
Check if you have /var/adm/cron/FIFO file with this settings:
root@aix14: /var/adm/cron # ls -l

prw------- 1 root cron 0 Jun 13 14:01 FIFO


If it is missing you can create it (don't forget p at the end):
mknod /var/adm/cron/FIFO p

Anonymous said...

Me too have found that .
It works fine when i remove the /var/adm/cron/FIFO and start the cron again.

but my issue is that how can I stop to happen this again ?
what causes to happen this ?

aix said...

I would open an IBM call to find out the real cause of it.

Anonymous said...

Change the letter at your site as they are difficult to be read.

aix said...

What is the problem with that? (small or not clear typed...?)

subbu said...

your blog is to useful to aix admins but explanation is very less to understand for nnew admins

aix said...

Yes it may be...I describe philosophy of this blog at the main page, where I mentioned it is not intended for beginners but experienced admins. After reading few IBM Redbooks and you visit back, probably you will find this blog much helpful :)

Unknown said...

hi...how to remove a cron job? .....(removing from file is a good way?)

aix said...

hi, yes, you open crontab file for edit (crontab -e), then remove unnecessary line(same way as in vi).

ra said...

hii Balazs,

my question:

if i add username in both cron.allow and cron.deny then which will override? whether the user will be finally allowed to schedule jobs or denied?

once again, thanks for this amazing blog...!

aix said...

Hi Rahul,
I just tested and cron.allow is the winner. (However a good sysadmin do housekeeping on them and will not use AIX this way :) )

Unknown said...

but this job is application dependant then what are the pre requsite for removing cron jobs?

Anonymous said...

I have added entry for user a_was8 in cron.allow but still user getting the error as not authorised, below snap. kindly suggest what could be the issue?
[root@test cron]# cat cron.allow
root
a_was8
adm[root@test cron]# cat cron.deny



[root@test cron]# ls -lrt
total 42184
-rw-r--r-- 1 root sys 1317 Jun 17 2004 queuedefs
-rw-r----- 1 bin cron 3 Jun 21 2004 cron.deny
-rw-r----- 1 bin cron 4 Jun 21 2004 at.deny
prw------- 1 root cron 0 Jan 11 2007 FIFO
-rw-r----- 1 bin cron 0 Jan 11 2007 at.allow
-rw-r--r-- 1 bin cron 16 Sep 19 21:10 cron.allow
-rw-rw-r-- 1 bin bin 21521851 Sep 19 21:35 log

[DMZ][a_was8@test a_was8]$ crontab -l
crontab: you are not authorized to use cron. Sorry.

aix said...

Hi, did you logout and login again? (You can check if temporarily rename cron.allow what happens.)

Anonymous said...

I did logout and login again but that didn't worked. I deleted cron.allow file and recreated it again and bounce the crond but that also didn't worked.

Anonymous said...

do we have any script or some to run a conjob on 1st sunday of every month.
can anyone help me

Luiza said...

Hello.
Your blog is very useful, tks.
Aix 7.1 was reinstalled and after this the execution of crontab reply with COMMAND NOT FOUND.
Do you know how to active the crontab service? Where is it?
Thank you.

Pavan barai said...

can any body tell me how to give permission to allow me to access crontab directory and files

Anonymous said...

how can i schedule a job that will execute when the system is going to reboot itself

Unknown said...

try in the rc.shutdown

Unknown said...

/usr/sbin/cron - command
/etc/inittab - location of cron to start

Unknown said...

Hi. You can check on the sample provided above. The syntax and samples
crontab file format:
minute hour day_of_month month weekday command
0-59 0-23 1-31 1-12 0-6 Sun-Sat shell command

* * * * * /bin/script.sh schedule a job to run every minute
0 1 15 * * /fullbackup 1 am on the 15th of every month
0 0 * * 1-5 /usr/sbin/backup start the backup command at midnight, Mo - Fr

example:
crontab -e 0 1 15 * * /script.exe description

lalit said...

i need to schedule 3 job in crontab ,first job should run every day on 2 am ,second job should run after success of first task and third job should run after success of second job, also need to collect status(success or failure) of jobs in a file plz help as i am new to aix

Unknown said...

cron jobs are getting failed, could you please help me what would be reason behind this issue?

/var/adm/cron:] tail -f log
Cron Job with pid: 35848204 Failed
Cron Job with pid: 32374994 Failed
root : CMD ( /usr/sbin/dumpctrl -k >/dev/null 2>/dev/null ) : PID ( 6160634 ) : Tue Feb 24 19:15:00 2015
root : CMD ( /usr/bin/echo "Test" ) : PID ( 42729628 ) : Tue Feb 24 19:15:00 2015
Cron Job with pid: 6160634 Failed
Cron Job with pid: 42729628 Failed
root : CMD ( /usr/sbin/dumpctrl -k >/dev/null 2>/dev/null ) : PID ( 31654086 ) : Tue Feb 24 19:20:00 2015
root : CMD ( /usr/bin/echo "Test" ) : PID ( 6422750 ) : Tue Feb 24 19:20:00 2015
Cron Job with pid: 31654086 Failed
Cron Job with pid: 6422750 Failed

Unknown said...

Cron logs are not generating in /var/adm/cron folder. can someone please help..

[host1:root:/var/adm/cron:] ls -lrt
total 32
-rw-r--r-- 1 root sys 1313 Sep 21 2009 queuedefs
-rw-r----- 1 bin cron 3 Mar 18 2011 cron.deny
-rw-r----- 1 bin cron 4 Mar 18 2011 at.deny
-rw-rw-r-- 1 bin bin 1245 Apr 23 12:14 log
-rw-rw-r-- 1 root cron 0 Apr 24 00:01 cronlog
prw------- 1 root cron 0 Jun 03 10:59 FIFO
[host1:root:/var/adm/cron:]


/etc/cronlog.conf file entry is "logfile=/var/adm/cron/cronlog"

Unknown said...

can any one help me what is the output of this command crontab|grep -i mksysb

Fawad Mirza said...

my code @reboot /usr/openv/netbackup/bin/bp.start_all in crontab is not working in aix system please help

Anonymous said...

how can i make a crontab job to run after every 15 mins??? please reply

aix said...

hi,
0,15,30,45 * * * * /path/to/command

Anonymous said...

On AIX, what scripts does cron run before running the script in the crontab file? I have a script entered in cron. On the crontab line I redirect the stdout to output.out and stderr to error.out in my home directory. When cron runs the job the error.out file contains line "sh: edtini.edt: not found.". I'm familiar with that particular file, but it's not referenced in the script that cron is running for me. I even put 'echo' statements at the top of my script, expecting to see that output in the output.out file, however the output.out file is empty. That's telling me that cron is running something else on my behalf before it runs the script in the crontab file. I thought maybe it was my .profile, so I added 'echo' statements to it as well. Again, nothing shows up in the output.out file. So, what file does cron run before it runs the script mentioned in the crontab file. Thanks. Richard

Anonymous said...

Disregard. Operator error. I had one too many * in the crontab file.

I had this to run the job every 15 minutes

0,15,30,45 * * * * * /path/script.ksh

It should read

0,15,30,45 * * * * /path/script.ksh

That last, extra, * was passing every file in the directory as the script file to run, and edtini.edt was the first file. It being the first file when I did an ls is what made me check the crontab entry more closely.

I hope someone else can learn from my carelessness.

aix said...

Thanks a lot for providing the update and solution :)

AI X said...

crontab -l | awk '{print} END {print "0 1 * * * /tmp/test.sh >> /tmp/test.log"}' | crontab => This only works if crontab was used befiore for that user because if not it will show an "can't open yourfile in /var/spool/cron/crontabs directory"

Thilagarajan Subramaniam said...

I got a question , i have a crontab that run everyday and i cant seem to locate that crontab in the list , can you help me to find that crontab

Anonymous said...

Hello there someone deleted cron.deny file , do I just use touch cron.deny and recreate the file. Also what are the defaults for that file

Unknown said...

How do we know,whether our cron job was running successfully or not