LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script not executing via cron (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-not-executing-via-cron-4175628674/)

yash1990 04-29-2018 04:52 PM

Shell script not executing via cron
 
Hi Team,

Recently there was system upgrade and after that, there are some commands that wont execute in shell script via cronjob, but they do execute if the shell script is executed manually.

Please help !!!!

Thanks,
Yashwant

eklavya 04-30-2018 01:10 AM

Make sure cron daemon is running.

Check timezone for system and cron, if cron is in different time zone, it runs but not on the time you expect.
https://stackoverflow.com/questions/...erent-timezone

Check cron logs, if it is not running, there should be an error.

yash1990 04-30-2018 04:05 AM

I dont think the issue is with timezone. Because there are multiple commands in that script, but only one command is not executing via cron. If that command is executed manually, then it works fine.

Turbocapitalist 04-30-2018 04:32 AM

Generic things to check would be whether the script has set its $PATH variable. The $PATH available inside cron is not the same one you have in your interactive shell session.

Code:

#!/bin/sh

set -e;

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin;

...


yash1990 04-30-2018 05:48 AM

As I am new to shell script, can you please let me know what should I do exactly. Unable to get anything releated to cron from env command.

this is what I can see in PATH section :

PATH=/opt/emc-tools/bin:/sbin:/usr/sbin:/usr/local/avamar/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/apache/bin:/usr/local/ssl/bin

rtmistler 04-30-2018 06:32 AM

To debug what the $PATH variable contains when running the script, you can echo any variable out to a log file.
Code:

echo "Path variable is: $PATH" >> /tmp/script-debug-output.log
Or some similar statement like that.

yash1990 04-30-2018 06:46 AM

Below is the output, when I manually execute the script :

Path variable is: /opt/emc-tools/bin:/usr/local/avamar/bin:/sbin:/usr/sbin:/usr/local/avamar/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/apache/bin:/usr/local/ssl/bin


and below is the output that I've got after executing script via cron :

Path variable is: /usr/bin:/bin

rtmistler 04-30-2018 07:17 AM

So that should tell you a lot.

HOWEVER!

Something just occurred to me about scripting that "I" follow. Can't say for others out there.

Since I'm horrible at "environments", what I do is when I write a script, I make sure that every system call is fully qualified in the path.

I.e. if I'm using ls, cp, grep, or some other binary. In the script when I call it out I use the full path:
/bin/ls
/bin/cp
/bin/grep
/usr/bin/md5sum

So that way I don't have problems finding executable variables.

The other way is to figure out how to set the environment properly for your cron job.

I'm sure others may write in with the knowledge there. It's never been my priority, as I'm demonstrating with my work-around/fix option.

yash1990 04-30-2018 07:28 AM

@rtmistler,

I am already using full path while executing commands, I dont know exactly where the issue is persisting from. Have already given a try by manually defining PATH variables in script, but still the issue is there.

rtmistler 04-30-2018 07:36 AM

That seems to be a near "pull out all the stops" form of debug needed. Seems as if something in the environment, and not the path variable, is the problem.

When running interactive, it works fine as you say, but perhaps you can learn some from it.

Sadly I'm not a cron person either, but probably should learn that more.

For a script, I use the "set -xv" to enable verbose debug, which helps when running it interactively. Meanwhile even if I run it out of a process, I sometimes find a way to re-direct stdout and stderr to a file. "$ ./scriptname 2>&1 filename" will redirect both stdout and stderr to a the filename. If there's a way to do that in cron, that will help you. And then turn the set -xv on to up the debug from the script.

Other options are to add a lot more debug, check progress in the script using debug, and check return variables, once again outputting that all to a log file.

In my signature, check the link for: My Bash Blog, I have a lot of this in that blog entry.

michaelk 04-30-2018 08:09 AM

I agree that a full debug is required. There are many variables that are unknown to us which makes troubleshooting difficult.

What do you mean by system upgrade?

Without knowing anything about the command that does not work i.e. something you created or an installed application, does it require any environment variables or options that require a specific path?

yash1990 04-30-2018 08:57 AM

@Michaelk:
System has been upgraded to latest version, but I don't think it does really matter in this situation.

Reason: It is only one command that doesn't execute via cron from the shell script, but that same command executes if executed manually. There are other commands that does get executed via cron (shell script), but problem is for only one.

The command that I am trying to execute is:
/usr/local/avamar/bin/mccli server show-prop

This command is used to get grid capacity and it is avamar backup command.

AwesomeMachine 04-30-2018 07:24 PM

Is that a binary or a shell script? If it's a shell script, then every command in the script should be changed to a fully qualified path. Just because you give a fully qualified path in crontab does not mean it is inherited by the shell script.

If it's a binary it might be in the wrong crontab. Your permissions might get mixed up somehow.

chrism01 04-30-2018 10:20 PM

As recommended above, make the top of the script
Code:

#!/bin/bash
set -xv

and the cron invocation something like
Code:

53 23 * * * /your_script.sh >/tmp/your_script.log 2>&1
The first will tell the parser to show you exactly what it's doing ('debug') and the 2nd will log all that stuff to check later.


All times are GMT -5. The time now is 03:23 AM.