LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-25-2014, 06:27 AM   #1
postcd
Member
 
Registered: Oct 2013
Posts: 527

Rep: Reputation: Disabled
Bash script dont works when executed as cronjob


Hello,

i have cronjob:

crontab -l
Code:
* * * * * pkill -f domexpcheck;sh /root/dom/domexpcheck.sh
it runs:

/var/log/cron
Code:
Mar 25 12:11:01 vps crond[17808]: (root) CMD (pkill -f domexpcheck;sh /root/dom/domexpcheck.sh)
but somehow script dont run properly via cronjob. But when i execute cronjob command (pkill -f domexpcheck;sh /root/dom/domexpcheck.sh) from command line it works good... (i receive test email), via cron i dont receive and appears that script wont do the job

stat /root/dom/domexpcheck.sh
Code:
  File: `/root/dom/domexpcheck.sh'
  Size: 5542            Blocks: 16         IO Block: 4096   regular file
Device: 17h/23d Inode: 23953953    Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-03-24 23:46:21.000000000 +0100
Modify: 2014-03-24 23:45:46.000000000 +0100
Change: 2014-03-24 23:45:46.000000000 +0100
whoami
Code:
root
head /root/dom/domexpcheck.sh
Code:
[root@vps /]
#pkill -f domexpcheck
set -ex
cd /root/dom
please any ideas why it dont works via cron?
 
Old 03-25-2014, 06:39 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,927

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
see the bottom of the page, "similar threads"
 
Old 03-25-2014, 10:11 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Clear your PATH in the shell and then try again

Code:
export PATH=/usr/bin:/bin
pkill -f domexpcheck;sh /root/dom/domexpcheck.sh
That should show you where the problem lies. Chances are you're not using the full path to an executable somewhere.
 
Old 03-27-2014, 08:08 AM   #4
yzT!
Member
 
Registered: Jan 2013
Distribution: Debian
Posts: 168

Rep: Reputation: 2
Quote:
Originally Posted by suicidaleggroll View Post
Clear your PATH in the shell and then try again

Code:
export PATH=/usr/bin:/bin
pkill -f domexpcheck;sh /root/dom/domexpcheck.sh
That should show you where the problem lies. Chances are you're not using the full path to an executable somewhere.
Just an addition:

"Somewhere" = pkill xD

OP change pkill for /usr/bin/pkill
 
Old 03-27-2014, 10:41 AM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by yzT! View Post
Just an addition:

"Somewhere" = pkill xD

OP change pkill for /usr/bin/pkill
As you said yourself, pkill is in /usr/bin, which is in cron's default path. It's something else, likely a call inside domexpcheck.sh.
 
Old 03-29-2014, 10:38 AM   #6
postcd
Member
 
Registered: Oct 2013
Posts: 527

Original Poster
Rep: Reputation: Disabled
Lightbulb

i followed your instructions, but script dont finish running probably when executed by cron.

in the script im using these commands:

set -ex
mail -s
awk
sed

also classic like "cp", "rm"

I tried to execute command from CLI: export PATH=/usr/bin:/bin
but it did not fixed it

Any idea pelase?
 
Old 03-29-2014, 11:07 AM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by postcd View Post
I tried to execute command from CLI: export PATH=/usr/bin:/bin
but it did not fixed it
It's not supposed to fix it...it's supposed to recreate cron's environment in your shell so that when you run the script it tells you why it's failing.
 
Old 04-01-2014, 06:30 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As hinted above, all cmds and files in a cronjob should be preceded by their full absolute path, unless you've taken other steps to ensure the env has everything it needs.
 
Old 04-04-2014, 02:52 PM   #9
postcd
Member
 
Registered: Oct 2013
Posts: 527

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
As hinted above, all cmds and files in a cronjob should be preceded by their full absolute path
thx, i have variables like:

Code:
cd /root/folder
variablename=filename
(so filename without its /full/path/filename) can this be the cause of cron failure?

or i need to do like /bin/cat instead of cat ?

Last edited by postcd; 04-04-2014 at 03:16 PM.
 
Old 04-13-2014, 12:12 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The key word is 'all'

You can do the var=fname if the file is definitely in the current dir.

See also the 2nd part of my previous 'unless ...'

Basically, when creating a script to be run from cron, assume NOTHING (you know what they say about 'assume' )
 
Old 04-14-2014, 11:36 AM   #11
postcd
Member
 
Registered: Oct 2013
Posts: 527

Original Poster
Rep: Reputation: Disabled
it worked when i deleted "pkill -f domexpcheck" from the cron command.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cronjob not running, however script it calls works ok JockVSJock Programming 5 09-16-2013 11:36 PM
[SOLVED] nc (netcat): script works when sourced but not if executed via its path Heraton Linux - Newbie 8 11-01-2012 06:35 PM
script per cronjob executed several times the_bigbalu Linux - Newbie 10 06-01-2012 01:14 AM
script executed inside another script dont make changes potraike Linux - Newbie 4 03-05-2008 05:27 AM
cronjob not working but script works dtra Linux - Newbie 4 02-28-2005 05:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:47 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
Open Source Consulting | Domain Registration