Bash script dont works when executed as cronjob


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash script dont works when executed as cronjob
# 1  
Old 03-25-2014
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?
# 2  
Old 03-25-2014
Cronjobs dont have access to the environment (eg: the $PATH variable, so it wont find 'ls' and others), so you must provide FULL & ABSOLUTE PATH for each command that is not a shell builtin.

However, you could also set a PATH, that will be used for that script only though.

Hope this helps
# 3  
Old 03-25-2014
Quote:
Originally Posted by sea
Cronjobs dont have access to the environment (eg: the $PATH variable, so it wont find 'ls' and others), so you must provide FULL & ABSOLUTE PATH for each command that is not a shell builtin.
That's not entirely accurate - using full path names is a good idea, but while cron won't execute the user profile, it will (if it's POSIX-compliant) set a basic default $PATH like /bin:/usr/bin. It also might run /etc/environment, although I'm not sure if that's POSIX or not (AIX and Debian seem to, anyway).

Last edited by CarloM; 03-25-2014 at 10:22 AM..
# 4  
Old 04-04-2014
thx, i have variables like:

Code:
cd /root/folder
variablename=filename

(so without /full/path/filename) can this be the cause of cron failure?
# 5  
Old 04-04-2014
Its not mandatory, but yes, might be the cause for errors.
However, i tend more to say its the multiple commands you want to execute.

So, i'd suggest make 2 scripts.
1 script to check whether script is already running, and if so closes it,
or otherwise calls the other one, domexpcheck.sh.

And execute just the 'new' script in crontab, which then will execute the domexpecheck.sh

hth
This User Gave Thanks to sea For This Post:
# 6  
Old 04-04-2014
Two or more commands in crontab are not a problem. What does not work? Does it kill the 'domexpcheck' processes but not start new ones? Check with
Code:
ps -ef | grep domexpcheck

# 7  
Old 04-05-2014
thx, I dont know why, but when i remove pkill command from cronjob line, script is executed and works..

Dont works:
Code:
* * * * * pkill -f domexpcheck;sh /root/dom/domexpcheck.sh

Code:
* * * * * pkill -f domexpcheck
* * * * * sleep 3;sh /root/dom/domexpcheck.sh

Works:
Code:
* * * * * sh /root/dom/domexpcheck.sh


Last edited by postcd; 04-05-2014 at 06:15 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command works interactively but not in bash script

The below command works in the terminal interactively but not as part of a bash script. I though maybe I needed to escape the "$dir" so it isn't interpreted literally, but that's not it. Thank you :). interactively in terminal dir=/path/to new=$(ls "$dir"/*.csv -tr | tail -n 1) && echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Linux/bash Script only working if executed from shell prompt

Hi, maybe I'm asking a VERY dumb question, but would anybody out there tell me, why this f****** script won't work if executed as a cronjob, but works fine if executed from a shell prompt? #! /bin/bash set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin date >>... (3 Replies)
Discussion started by: beislhur
3 Replies

3. Shell Programming and Scripting

Script in bash that works only some of the time

I ran this script yesterday (in the background) /usr/bin/nohup myfilelocation/myscriptname.sh & the script worked perfectly. i ran it today (also in the background) and just sat there. So i killed it and ran it normally and it worked perfectly. Anyone suggest why it just sat there and... (8 Replies)
Discussion started by: twinion
8 Replies

4. Shell Programming and Scripting

Calling bash script works when called manually but not via Cron?

Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin. Basically the path structure is ... (6 Replies)
Discussion started by: wyclef
6 Replies

5. AIX

Script not getting executed via cron but executes when executed manually.

Hi Script not getting executed via cron but executes successfully when executed manually. Please assist cbspsap01(appuser) /app/scripts > cat restart.sh #!/bin/ksh cd /app/bin date >>logfile.out echo "Restart has been started....." >>logfile.out date >>logfile.out initfnsw -y restart... (3 Replies)
Discussion started by: samsungsamsung
3 Replies

6. Shell Programming and Scripting

can't executed bash from PHP..

dear list, i've just write a simple php script to add user to my iptables, but some times it works and it dosn't work,, here's my script for my adduser.php <?php exec('/var/www/html/backup.sh'); $hash = "# $_POST\n"; $ip = "-A INPUT -p tcp -m tcp -s $_POST -d 10.10.105.18 --dport 8080 -j... (2 Replies)
Discussion started by: ridwanfi
2 Replies

7. Shell Programming and Scripting

Script works with bash 3.0 but not 3.2.

Hello, So my knowledge of bash scripting is not that great and I have been trying to solve this problem on my own for awhile to no avail. Here's the error I get when running it with an OS that uses bash 3.2.x: testagain.sh: line 10: *-1: syntax error: operand expected (error token is... (2 Replies)
Discussion started by: forkandspoon
2 Replies

8. Shell Programming and Scripting

Works from bash prompt, but not from script!

I'm trying to use unison from bash on windows with cygwin. I don't know if this is a cygwin question, bash question or unison question. Since I always get reprimanded by the cygwin mailing list for assuming it is a cygwin problem, I'll assume it is a bash question. The following commands work... (7 Replies)
Discussion started by: siegfried
7 Replies

9. Shell Programming and Scripting

how to make a bash script that can be executed by people simultaneously?

dear friends, i want to make a bash script that can be executed by many people simultaneously. do you have any idea to make it? there will be many dependent-variables(which is input from people) in the scripts. i am thinking about a random temporary file that created by the bash script each... (4 Replies)
Discussion started by: jimmbp
4 Replies

10. Shell Programming and Scripting

bash scripting cannot executed in crontab

hi guys, i have a problem. a week ago i made a successful crontab that execute bash scripting daily, it worked well but now, it doesn't work at all, in the mail i have: " /home/jimmy/cha/scripts/cekpderr produced the following output: lagi jalan /home/jimmy/cha/scripts/cekpderr:... (6 Replies)
Discussion started by: jimmbp
6 Replies
Login or Register to Ask a Question