0

I have a script which gives output in a file and doing few awk operations on that output file and mixing the output of two files using paste -d and then sending the email of newly created file to email. If i run same script from command line, its giving output and sending email as well, but if i use same command for cron job. Its not sending email and /var/log/cron gives this message. "CROND[6235]: (root) MAIL (mailed 125 bytes of output but got status 0x0043#012)". No info for message code (0x0043#012). Please suggest.

Romeo Ninov
  • 3,195
  • 2
  • 13
  • 16
Rahul
  • 1
  • Does your cron job work with SELinux in permissive mode (`setenforce 0`)? Could you also provide the code snippet that sends the e-mail? – Anderson Medeiros Gomes Mar 05 '20 at 08:11
  • @AndersonMedeirosGomes: Yes it does. ./k1.sh | tee new-k1.txt && paste new-k1.txt old-k1.txt | awk '{ printf("%d\n", ($3-$6)); }' | tee final-k1.txt && paste -d "" prefix-K1.txt final-k1.txt | tee code-k1.txt && cat header-k1.txt code-k1.txt | mail -s "Response-Code-K1" username@XXXXX.com – Rahul Mar 05 '20 at 08:59
  • I see that you are scheduling cron jobs via `crontab -e`. I initially imagined that you were creating entries in `/etc/crontab` file. Please, could you edit your question and add both crontab entries there (the one who is working and the one who is failing to send e-mails)? Could you also retrieve any related SELinux denial messages by running `ausearch -ts recent -sv no`? – Anderson Medeiros Gomes Mar 05 '20 at 19:17

1 Answers1

0

From question's comments section, I see that SELinux is preventing your cron job from sending e-mails. The denial does not happen on a manual run from command line because SELinux does not confine interactive sessions by default. Jobs launched by Cron, on the other side, are confined.

I am not sure about the reason why a SELinux denial has been generated in your CentOS 7 system. I tried to run mail command from Cron in a fully-updated CentOS 7 system and the message has been sent successfully. I suggest you to ensure that your server has the latest packages installed (yum upgrade) and perform a SELinux relabeling in entire filesystem (touch /.autorelabel && reboot).

Follow standard SELinux troubleshoot steps in case your cron job continues to fail after the relabeling. Or, instead, you may configure permissive mode permanently:

  • Option 1: Configure SELinux permissive mode on cron service:
# semanage permissive -a crond_t
  • Option 2: Configure SELinux permissive mode on entire system:
# sed -ri.bak 's/^(SELINUX=).*/\1permissive/' /etc/selinux/config && reboot
  • Other cron job are working fine and sending email. Ex:- */1 * * * * ./k1.sh | tee test-k1.txt | mail -s "Test-k1" user@XXXXX.com – Rahul Mar 05 '20 at 11:53