LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2018, 04:52 PM   #1
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 15

Rep: Reputation: Disabled
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
 
Old 04-30-2018, 01:10 AM   #2
eklavya
Member
 
Registered: Mar 2013
Posts: 626

Rep: Reputation: 137Reputation: 137
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.
 
Old 04-30-2018, 04:05 AM   #3
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-30-2018, 04:32 AM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 3,068
Blog Entries: 3

Rep: Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327
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;

...
 
Old 04-30-2018, 05:48 AM   #5
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-30-2018, 06:32 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 6,615
Blog Entries: 12

Rep: Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296
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.
 
Old 04-30-2018, 06:46 AM   #7
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-30-2018, 07:17 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 6,615
Blog Entries: 12

Rep: Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296
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.
 
Old 04-30-2018, 07:28 AM   #9
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 15

Original Poster
Rep: Reputation: Disabled
@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.
 
Old 04-30-2018, 07:36 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 6,615
Blog Entries: 12

Rep: Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296Reputation: 2296
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.
 
Old 04-30-2018, 08:09 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,960

Rep: Reputation: 2140Reputation: 2140Reputation: 2140Reputation: 2140Reputation: 2140Reputation: 2140Reputation: 2140Reputation: 2140Reputation: 2140Reputation: 2140Reputation: 2140
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?
 
Old 04-30-2018, 08:57 AM   #12
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 15

Original Poster
Rep: Reputation: Disabled
@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.
 
Old 04-30-2018, 07:24 PM   #13
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 4,578

Rep: Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844
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.
 
Old 04-30-2018, 10:20 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,501

Rep: Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem executing commandline from script via cron battles Linux - Newbie 3 09-04-2014 10:14 AM
cron not executing a script KroniK Linux - Newbie 4 01-07-2014 08:26 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
Issues while executing shell scripts through Cron job paragkalra Programming 8 08-02-2008 01:18 PM
Cron not Executing PHP script Badnoodles Linux - General 2 10-25-2006 04:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration