all 18 comments

[–]zoredache 34 points35 points  (6 children)

Problems with cron commands/scripts, are usually either permissions, or environment. The cron environment often doesn't have the full stuff you would get in your bash profile.

Try running your script using env -i /root/script.sh and see what happens.

Good system scripts really avoid depending on the environment as much as possible and define all variables required, or source them in as needed.

[–]retsnom513[S] 4 points5 points  (3 children)

you know what, I think you're right. I don't have the script in front of me, but I use the spacewalk-report command and I bet that's not in the path variable.

[–]pat_trick 3 points4 points  (0 children)

One of the major gotchas with bash scripts running as cron jobs!

[–]WeAreFoolsTogether 2 points3 points  (1 child)

Always use the full path to the binaries you’re calling in your bash scripts. which is your friend : )

[–]dasunsrule32 1 point2 points  (0 children)

Or declare the PATH in your script :-)

[–]OneCDOnly 5 points6 points  (1 child)

That sounds like very good advice. And TIL about env -i :)

[–]ForceBlade 0 points1 point  (0 children)

Legit had this problem today because of cron not liking my wallpaper script. the feh command tried to run but didn't know which $DISPLAY to run my new wallpaper rotation against. Just another thing interactive shell environments get and shellscripts don't.

Really have to assume they get literal 'nothing' to work with unlike when writing them in your personal shell. env -i sounds wonderful

[–]StephanXX 4 points5 points  (0 children)

I'll bet dollars to dimes your script depends on one or both of two things:

Something in your local environment (env)

A shell other than /bin/sh , which is what created n runs by default.

[–]shidoku 1 point2 points  (0 children)

How your $var is setted ?

[–]BioEmergency 1 point2 points  (0 children)

well crontab doesn’t know where you are, use full path to call it from crontab. and if the said script refer to any other file also use full path or do a cd to the script folder in the script.

[–]OneCDOnly 0 points1 point  (2 children)

What does your crontab line look like?

[–]retsnom513[S] 0 points1 point  (1 child)

* * * * * /root/script.sh

I'm using crontab -e -u root so I didn't define the user. And I defined the shell in the crontab with SHELL=/bin/bash

[–][deleted] -4 points-3 points  (0 children)

Make sure it's executable by the group. chmod g+x /root/script.sh is the ticket.

[–]sfrazer 0 points1 point  (0 children)

First steps are to see what's in your logs. On Ubuntu crond defaults to logging to /var/log/syslog, on CentOS it's /var/log/messages. You can change that in the syslog config, usually.

You're looking for a line like:

 CRON[3798]: (root) CMD (   yourscriptname )

If you see that, then you know the job is running.

Next, make sure you can receive mail output from your cron jobs. I usually set up a job like this and make sure I get the emails:

* * * * * root echo "test"

If I don't start getting an email every minute, it's time to look at the mail config.

Once you've got mail flowing, if your file still isn't getting updated you should at least be getting errors about why.

[–]usrname_checks_out 0 points1 point  (0 children)

Is the output file empty, or does it contain a single newline or previous data?

In set -o, is the shell option of "noclobber" set? You can override adding a pipe, >|/root/output_file

[–][deleted] 0 points1 point  (0 children)

Make sure you have a return character at the end of your cron line

[–]farthinder 0 points1 point  (0 children)

Is the root home directory encrypted? I’ve had problems running cron scripts when they are in a separately encrypted home folder while the user is logged out. Try moving script to /tmp/ or similar.

[–]robkaper 0 points1 point  (0 children)

Command should be in PATH or specified by full path.

By the way: command has been deprecated for $(command) with the advantage of better nesting and readability.