Technical Discussion
  >> Linux Issues


Register (or login) on our website and you will not see this ad.


  Print Thread
Standard User think26872
(committed) Tue 07-Feb-12 19:55:38
Print Post

Script fine at prompt but cron fails when using backticks?


[link to this post]
 
I have a bash script that works fine at the prompt (Ubuntu).

When the script is run in the crontab it fails after a while when it runs an external program using the backticks e.g. `date` or when it runs a another script.

If I remove all the backtick lines or $(date) lines or script.pl/sh lines the script works fine in the crontab.

Any ideas what could be causing this? I have checked for the same PATH environments and they are fine.

TIA
Standard User mixt
(experienced) Tue 07-Feb-12 20:16:48
Print Post

Re: Script fine at prompt but cron fails when using backtick


[re: think26872] [link to this post]
 
Had this problem before with cron. It can be fussy if you start trying to perform complicated "one-liners" in the crontab file.

Your easiest safest bet is to take your "complex" command, and put it in a file so it can be run as a script. Then reference that file in the crontab file. This is guaranteed to work.

If you don't want to do that, turn on debug output in crond so you can see exactly what it's complaining about in /var/log/messages (or where ever your crond logs get written).

Now on <aaisp.net> (21CN+IPv6)
Previous ISPs: Virgin Media (50Mb/Cable), Be* Un Limited, ZeN
Is Linux routing your internet connection?
Need to make BIND geo-aware?

Edited by mixt (Tue 07-Feb-12 20:18:01)

Standard User think26872
(committed) Tue 07-Feb-12 20:49:31
Print Post

Re: Script fine at prompt but cron fails when using backtick


[re: mixt] [link to this post]
 
Thanks, sorry if I was not clear the crontab is running a script (quite a few lines long).

It is weird I have just gone into the script and reduced the number of `` entries in various functions and the script works fine in the crontab.

It is like as soon as it has processed around 20 `` entries the script just stop working. It stops logging to a log file and just appears to give up.

Any other ideas?


Register (or login) on our website and you will not see this ad.

Standard User mixt
(experienced) Tue 07-Feb-12 21:39:00
Print Post

Re: Script fine at prompt but cron fails when using backtick


[re: think26872] [link to this post]
 
You could always try replacing the back-ticks with $(command).

So rather than:

`command`

you have:

$(command)

but that will only work inside bash (I think). So...

You can do a complex command in the crontab file if you call bash followed by what you want to run (remember, the command that cron runs is just that - the direct command, it's not running inside a bash shell). So you could try something like this as the line in the crontab file:

0 0 * * 1-5 /bin/bash -c "cmd1; cmd2; cmd3"

Without looking precisely at what's going on, I'm unable to make any further suggestions really.

Now on <aaisp.net> (21CN+IPv6)
Previous ISPs: Virgin Media (50Mb/Cable), Be* Un Limited, ZeN
Is Linux routing your internet connection?
Need to make BIND geo-aware?

Edited by mixt (Tue 07-Feb-12 21:40:56)

Standard User pmb00cs
(eat-sleep-adslguide) Wed 08-Feb-12 20:19:50
Print Post

Re: Script fine at prompt but cron fails when using backtick


[re: think26872] [link to this post]
 
Debugging the cron job is a good idea.

What is your default shell?

What is the shell variable for the user you are using to run the script when it works?

What version of Linux?

What is the #! at the top of the script you are calling from cron?

what Memory constraints does the system have?

happily chugging along on plusnet and Virginmedia (yes I am greedy)
My web server
  Print Thread

Jump to