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

Here's the content of my crontab file:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO="example#example.com"
*/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?

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.

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.

simple answer
on your crontab you need to specify the USER to run the command
example to run as ROOT is:-
0,10,20,30,40,50 * * * * root /path_to_script/script_name
or to run as user FRED
0,10,20,30,40,50 * * * * fred /path_to_script/script_name
default with no USER specified is to run as user CRON and that user would not have permissions to execute the script

We can create cron jobs for system as well for individuals. The crontab in /etc/crontab specifically used for system cronjobs. So you need to specify the cronjob command executed by whom. In the question the username not specified. Hence the ERROR (getpwnam() failed) occurs. You can create user specific cronjobs in /var/spool/cron/username
NOTE:: Cron jobs are very useful but disastrous on failures!

Nothing is wrong with the crontab file (so long as by "my" crontab, you mean that it's a user crontab rather than a system crontab; otherwise, see other answer).
On the other hand, something is wrong with your system's directory service -- as configured, in Linux, with nsswitch.conf. Perhaps you're using a Kerberos-authenticated LDAP store, and your cron daemon doesn't have a Kerberos token to connect to it (or is sandboxed, as with SELinux, not to have network access); perhaps it's a file store that isn't readable by the user whose crontab is being run; perhaps some other odd and interesting thing is going on.
getpwnam() is a C library call that performs a lookup for the name of the currently-logged-in user. If your shell were bash, it would fall back to a name of I have no name! -- so this error means your sh implementation is something different. (If you want to run your scripts with bash, use bash, not sh).

Related

Output not going to file when ran as a cron job

I have the following bash script:
clean-tmp.sh
#!/bin/bash
tmpreaper 1h /tmp --test > ./tmpreaper.log
When I run it in the terminal using ./clean-tmp.sh, it writes to the file ./tmpreaper.log.
I added the script to the list of cron jobs using crontab -e:
*/5 * * * * cd /home/cron-jobs && ./clean-tmp.sh
I then checked cron's logs and this entry is in there every 5 minutes:
Feb 19 00:45:01 ip-172-31-23-184 CRON[1475]: (ubuntu) CMD (cd /home/cron-jobs && ./clean-tmp.sh)
But it's no longer writing to ./tmpreaper.log.
What on earth am I doing wrong?
Just specify an absolute path for your file, like tmpreaper 1h /tmp --test > /var/log/tmpreaper.log
#Kacy: Little difficult to say without cron logs, you could have a look to cron logs(/var/log/cron etc).
0,5,10,15,20,25,30,35,40,45,50,55 * * * * cd /home/cron-jobs; ./clean-tmp.sh
May be some systems wouldn't allow time period as the way you tried, try once in above way and let us know then.

El Capitan: CRON Job is unable to trigger a shell script

*/10 * * * * /bin/sh /dataScience/dat/JOB/forker.sh
Above is the cron job which does not execute. I have checked separately both the functioning of cron job and shell script which works fine. Even the below command works well.
/bin/sh /dataScience/dat/JOB/forker.sh
I have also checked the permissions of forker.sh which shows
-rwxr-xr-x 1 mango wheel 442 Nov 20 12:53 forker.sh
Am i missing something which restricts the cron job from triggering the script?
Generally the syntax for the job goes like:
minute hour dom month dow user cmd
In your case, the job might be for multiple user, it seems
*/10 * * * * /bin/sh /dataScience/dat/JOB/forker.sh
Which implies it should be run at the interval of 10 mins all the time. Which is not problematic.
Can you tell us if the following is giving you the output at all?
*/10 * * * * (echo "Printing."; echo "Logging." > /dataScience/dat/JOB/abc.txt | mail -s "Mailing" your_email)
Note: I wanted to comment this, since this is not exactly the answer but debugging, but I have reputation lesser than 50, hence couldn't comment.

How to run Bash Script in Crontab on Raspbian?

I have gone through all sorts of answers and replies up and down the Internet and nothing to seem to work for me. I want to simply run a bash script every minute using CRONTAB on Raspberry PI on Raspbian.
I have a script called autocon.sh and I simply entered into crontab as follows:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
...
1 * * * * root bash /home/pi/autocon.sh
BUT IT WON'T RUN.
What am I doing wrong?
I'm not sure where the 'root' part comes from, but I'm guessing you're wanting to run the script as root? If so, you need to put an entry into the root crontab, do so by running:
sudo crontab -e
This will open up the root user crontab for editing, anything run from said location will run with root priveleges.
Insert the following line:
* * * * * bash /home/pi/autocon.sh
That should do it :) The 1 in your script actually means 'run at 1 minute past the hour' and thus in your case, 1 minute past every hour - easy mistake! Replacing it with a * means every minute.
The syntax is:
minute - hour - day of month - month - day of week - command
Additionally, if you make your script executable, like so:
sudo chmod +x /home/pi/autocon.sh
You can omit the 'bash' command, and simply use:
* * * * * /home/pi/autocon.sh
And unless you're using the two lines at the top for something in particular, you can omit those too.
For clarity, Barmar's comment on my original post:
In per-user crontab files you don't put the username. But in
/etc/crontab you do.
To run a cronjob every minute, all the values must be stars. Your cronjob is set to run at 1 minute past the hour every hour.
It should be:
* * * * * root bash /home/pi/autocon.sh

Bash Script with Crontab

I'm trying to setup a crontab I have this in my current job in the current user I'm logged into
* * * * * /CS/day/get_info.sh
get_info.sh is supposed to output a text file every minute and I suspected that it would output a file in the same directory as the script is located but it doesn't.
I've also checked the syslogs to see if I could figure this out.
(user) CMD (/CS/day/get_info.sh)
(user) MAIL (mailed 46 bytes of output but got status 0x0001#012)
Can someone explain to me why this is happening?
Thanks
man cron tells you:
When executing commands, any output is mailed to the owner of the
crontab (or to the user named in the MAILTO environment variable in the
crontab, if such exists). The children copies of cron running these
processes have their name coerced to uppercase, as will be seen in the
syslog and ps output.
So you have to
cd into the appropriate directory yourself (cron will use $HOME)
redirect ANY output to a file of your choice
You can do both things in the crontab. But I recommend to do it in the first lines of the script itself:
#!/bin/bash
cd WHEREEVER_YOU_WANT
exec > YOUR_LOG_FILE 2&>1
The script is run in the home directory of the user and the file should be there as well. If you want it in the same directory as the script, either do a cd in your script or modify your crontab entry:
*/1 19-20 * * * cd /CS/day; /CS/day/get_info.sh
Another common problem with crontab entries is the environment. If the script works correctly in your terminal, try debugging it, when it is run from cron:
40 11 * * * bash -x /CS/day/get_info.sh >/tmp/get_info.sh.log 2>&1
Run it once only with current time, because otherwise you will overwrite your log file every minute.
On my case, I just had to install and configure an smtp client.

crontab is not running my script

I'm new to cron jobs. I read a post on how to write a cron job with crontab.
So my crontab looks like this:
1 * * * * /Users/apple/Desktop/wget/down.sh
which basically means that every minute i want to execute the script :down.sh. Now the script runs
fine manually. The script is a simple program that downloads a PDF from the internet:
#!/bin/bash
wget -U Mozilla -t 1 -nd -A pdf "http://www.fi.usj.edu.lb/images/stories/HoraireS08/3eli.pdf" -e robots=off;
I don't know why it's not running every minute once the terminal tells me that he's installing the new crontab.
Can somebody help me please?
Solution:
Thank you all for your help, the syntax as mcalex said should be
* */1 * * * path/to/script
if you want it to be executed every hour.
The cron job was working normally.However my mistake was simply writing permissions, in fact while executing the wget command, it's supposed to write the pdf file in the current workind directory which is a system directory in case of the cron tab. so i solved my problem simply by navigating to the Desktop directory before executing the wget command like so:
cd /Users/apple/Desktop/wget
and then do whatever i want to do.
PS: i should include the full path of the wget command too.
Thank you all for you help again:)
When you put 1 in the first column, it will run on the first minute (of every hour). In order to get it to run in every minute of every hour, you need to set the minute column as */1
So your line should read:
*/1 * * * * /Users/apple/Desktop/wget/down.sh
supporting links:
job every minute: https://bbs.archlinux.org/viewtopic.php?id=59180
job every 5 minutes: http://www.thegeekstuff.com/2011/07/cron-every-5-minutes/
1 * * * * /Users/apple/Destop/wget/down.sh
From this entry script will never run on every minute because it will run on first minute of every hour.
Make this change to your crontab file to run this script every min.
"* * * * * /Users/apple/Destop/wget/down.sh"
Do you have a typo? It looks like you might have mis-typed Desktop?
Another thing to do is to redirect the output of running the script to a file so you can see what's going on like this:
1 * * * * /Users/apple/Destop/wget/down.sh >> /tmp/cron.out
and then check out the file to see what's going on.
Did cron send you some email detailing what went wrong?
Does the script under that path exist?
Note that cron uses /bin/sh to execute commands.
Did you set a proper PATH in your script? wget may not be in the default PATH or there may be no PATH at all. Try using /path/to/wget in the script.
Note that downloading the same PDF file once a minute is probably a silly idea, though...
If your cronjob is writing things to disk then be careful that system preference "Put hard disk to sleep when possible" is unchecked.
This was preventing my cronjob backup tasks to execute.

Resources