Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"getpwnam() failed" in /bin/sh only when called from cron

Here's the content of my crontab file:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO="[email protected]"

*/5 * * * * sh /robot/1/master.sh >/dev/null 2>&1
*/5 * * * * sh /robot/2/master.sh >/dev/null 2>&1
*/5 * * * * sh /robot/3/master.sh
*/5 * * * * sh /robot/4/master.sh >/dev/null 2>&1
*/5 * * * * sh /robot/5/master.sh >/dev/null 2>&1

This is the error that keeps showing in /var/log/cron when it tries to run:

crond[669]: (sh) ERROR (getpwnam() failed)

If I run any of these files manually, they work without any issues.

What's wrong with the crontab file?

like image 784
Andrew Avatar asked Jan 25 '17 21:01

Andrew


2 Answers

It surprises me that nobody has the correct answer to this. Today i faced exactly the same problem and google didn't help.

After 2 hours i found that when placing a file in /etc/cron.d the schedule line has to contain an extra option.....

I allways use this for my crontab -e

# Minute   Hour Day of Month     Month          Day of Week     Command    
# (0-59)  (0-23)   (1-31)  (1-12 or Jan-Dec)  (0-6 or Sun-Sat)  /my/fancy/script.sh            

So it contains 6 items.

When placing this in a file inside /etc/cron.d the cron needs an extra option, being the user to run your fancy/script.

# Minute   Hour Day of Month     Month          Day of Week     Who   Command    
# (0-59)  (0-23)   (1-31)  (1-12 or Jan-Dec)  (0-6 or Sun-Sat)  root  /my/fancy/script.sh            

This is documented in man crontab(5). For example https://linux.die.net/man/5/crontab . It says:

Jobs in /etc/cron.d/

The jobs in cron.d are system jobs, which are used usually for more than one user. That's the reason why is name of the user needed. MAILTO on the first line is optional.

like image 190
hetOrakel Avatar answered Oct 15 '22 05:10

hetOrakel


The sixth position is reserved for username running the job. You specified a user called sh which is most probably not present on the machine.

like image 4
Yuri Avatar answered Oct 15 '22 05:10

Yuri