IBM Support

AIX CRON: How to capture cron job errors

Question & Answer


Question

My cron job is failing, but there is no indication of root cause. How can I get more information?

# cat /var/adm/cron/log
root      : CMD ( /usr/bin/ksh -x /tmp/testA.sh >> /tmp/testA.out  2>>/tmp/testA.err  ) : PID ( 4653170 ) : Wed Aug 18 20:17:00 2021
Cron Job with pid: 4653170 Failed

Answer

Cron reports a failure if any command exits with a nonzero return. Cron sends STDOUT and STDERR for the job, unless it is redirected elsewhere.
You can quickly parse mail files to determine if cron sent mail to the user.
# egrep "\"cron\" job| cron job" /var/spool/mail/<userid>
Examine the mail file for more details if applicable.
If there is no email sent to the crontab user, then check the application for STDOUT and STDERR. 
See examples in the following five scripts.
The cron jobs:
root@hostnameA #crontab -l
* * * * * /usr/bin/ksh -x /tmp/testA.sh >> /tmp/testA.out  2>>/tmp/testA.err # STDERR and STDOUT defined in crontab
* * * * * /usr/bin/ksh -x /tmp/testB.sh # STDERR and STDOUT handled by cron default
* * * * * /usr/bin/ksh -x /tmp/testC.sh  # STDERR and STDOUT handled in script
* * * * * /usr/bin/ksh -x /tmp/testD.sh  >/dev/null 2>&1 # SILENCE STDERR and STDOUT
* * * * * /usr/bin/ksh -x /tmp/testE.sh  >/dev/null # STDERR ONLY

 
The scripts:
# cat /tmp/testA.sh
#!/bin/sh
touch /tmp/testA.file
rm /tmp/testA.file
ls -al /tmp/testA.file
# cat /tmp/testB.sh
#!/bin/sh
touch /tmp/testB.file
rm /tmp/testB.file
ls -al /tmp/testB.file
# cat /tmp/testC.sh
#!/bin/sh
touch /tmp/testC.file >> /tmp/testC.out  2>>/tmp/testC.err
rm /tmp/testC.file >> /tmp/testC.out  2>>/tmp/testC.err
ls -al /tmp/testC.file >> /tmp/testC.out  2>>/tmp/testC.err
# cat /tmp/testD.sh
#!/bin/sh
touch /tmp/testD.file
rm /tmp/testD.file
ls -al /tmp/testD.file

 
# cat /tmp/testE.sh
#!/bin/sh
touch /tmp/testE.file
rm /tmp/testE.file
ls -al /tmp/testE.file

 
The cron log file:
# cat /var/adm/cron/log
root      : CMD ( /usr/bin/ksh -x /tmp/testA.sh >> /tmp/testA.out  2>>/tmp/testA.err #STDERR and STDOUT defined in crontab  ) : PID ( 6684978 ) : Wed Aug 18 20:52:00 2021
root      : CMD ( /usr/bin/ksh -x /tmp/testB.sh # STDERR and STDOUT handled by cron default ) : PID ( 4653456 ) : Wed Aug 18 20:52:00 2021
root      : CMD ( /usr/bin/ksh -x /tmp/testC.sh  # STDERR and STDOUT handled in script ) : PID ( 7209426 ) : Wed Aug 18 20:52:00 2021
root      : CMD ( /usr/bin/ksh -x /tmp/testD.sh  >/dev/null 2>&1 # SILENCE STDERR and STDOUT ) : PID ( 5177642 ) : Wed Aug 18 20:52:00 2021
root      : CMD ( /usr/bin/ksh -x /tmp/testE.sh  >/dev/null #STDERR ONLY ) : PID ( 7012852 ) : Wed Aug 18 20:52:00 2021
Cron Job with pid: 4653456 Failed
Cron Job with pid: 7209426 Failed
Cron Job with pid: 6684978 Failed
Cron Job with pid: 5177642 Failed
Cron Job with pid: 7012852 Failed
 
The STDOUT and STDERR from cron:
testA.sh: STDERR and STDOUT defined in crontab:
# cat /tmp/testA.out
(empty)
# cat /tmp/testA.err
+ touch /tmp/testA.file
+ rm /tmp/testA.file
+ ls -al /tmp/testA.file
ls: 0653-341 The file /tmp/testA.file does not exist.
# mail
(no mail messages from cron)
testB.sh: STDERR and STDOUT handled by cron default
# cat /tmp/testB.out
(empty)
# cat /tmp/testB.err
(empty)
# mail
From daemon Wed Aug 18 20:52:00 2021
Date: Wed, 18 Aug 2021 20:52:00 -0500
From: daemon
To: root
Subject: Output from cron job /usr/bin/ksh -x /tmp/testB.sh # STDERR and STDOUT handled by cron default, root@hostnameA, exit status 2
Cron Environment:
 SHELL = /usr/bin/sh
 PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin:/usr/java8_64/jre/bin:/usr/java8_64/bin:/usr/local/bin
 CRONDIR=/var/spool/cron/crontabs
 ATDIR=/var/spool/cron/atjobs
 LOGNAME=root
 HOME=/
Your "cron" job executed on hostnameA on Wed Aug 18 20:52:00 CDT 2021
/usr/bin/ksh -x /tmp/testB.sh # STDERR and STDOUT handled by cron default

produced the following output:
+ touch /tmp/testB.file
+ rm /tmp/testB.file
+ ls -al /tmp/testB.file
ls: 0653-341 The file /tmp/testB.file does not exist.

*****************************************************************
        cron: The previous message is the standard output
        and standard error of one of the cron commands.

 
testC.sh:STDERR and STDOUT handled in script:
# cat /tmp/testC.out
(empty)

# cat /tmp/testC.err 
ls: 0653-341 The file /tmp/testC.file does not exist.
# mail
(no mail messages from cron)
testD.sh: SILENCE STDERR and STDOUT:
No error messages or emails for testD.sh
testE.sh: STDERR ONLY:
From daemon Wed Aug 18 20:52:00 2021
Date: Wed, 18 Aug 2021 20:52:00 -0500
From: daemon
To: root
Subject: Output from cron job /usr/bin/ksh -x /tmp/testE.sh  >/dev/null #STDERR ONLY, root@hostnameA, exit status 2
Cron Environment:  SHELL = /usr/bin/sh  PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin:/usr/java8_64/jre/bin:/usr/java8_64/bin:/usr/local/bin  CRONDIR=/var/spool/cron/crontabs
 ATDIR=/var/spool/cron/atjobs
 LOGNAME=root
 HOME=/
Your "cron" job executed on hostnameA on Wed Aug 18 20:52:00 CDT 2021
/usr/bin/ksh -x /tmp/testE.sh  >/dev/null #STDERR ONLY
produced the following output:
+ touch /tmp/testE.file
+ rm /tmp/testE.file
+ ls -al /tmp/testE.file
ls: 0653-341 The file /tmp/testE.file does not exist.

*****************************************************************
        cron: The previous message is the standard output
        and standard error of one of the cron commands.

 
SUPPORT

If you require more assistance, use the following step-by-step instructions to contact IBM to open a case for software with an active and valid support contract.  

1. Document (or collect screen captures of) all symptoms, errors, and messages related to your issue.

2. Capture any logs or data relevant to the situation.

3. Contact IBM to open a case:

   -For electronic support, see the IBM Support Community:
     https://www.ibm.com/mysupport
   -If you require telephone support, see the web page:
      https://www.ibm.com/planetwide/

4. Provide a clear, concise description of the issue.

 - See: Working with IBM AIX Support: Describing the problem.

5. Collect a system snap with cron data.

# snap -r (removes previous snap data)
# snap -aZ (excludes system dump)
# tar -cvf /tmp/ibmsupt/testcase/cron.tar -C /var adm/cron spool/cron -C /etc cronlog.conf
# ls -al /usr/sbin/cron /usr/bin/crontab /usr/bin/at > /tmp/ibmsupt/testcase/cronfiles.ls
6. Prepare the snap for upload.
# snap -c (Compress snap)
# mv /tmp/ibmsupt/snap.pax.Z /tmp/ibmsupt/yourcase#.snap.pax.Z (Rename file to associate with your case)
7. Upload all of the details and data for your case.
RECOMMENDED:
 
a) Attach to your case
 
b) Upload to the Enhanced Customer Data Repository(ECuRep)
 
ADDITIONAL OPTIONS:
 
c) Blue Diamond customer data uploads (US only)

[{"Type":"MASTER","Line of Business":{"code":"LOB08","label":"Cognitive Systems"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG10","label":"AIX"},"ARM Category":[{"code":"a8m0z000000cvzgAAA","label":"Commands"}],"ARM Case Number":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Versions"}]

Document Information

Modified date:
02 August 2022

UID

ibm16482433