user347918
user347918

Reputation: 161

not calling php script from shell script which installed in crontab

i have move_files.sh and installed it in crontab.

Actually job is working because it's printing those echo. And creating log file. But it's not calling that PHP script. Interesting thing is if i run it by manually it's calling php script and working 100%. But why it's not calling after i installed it on crontab. should i put "php" before calling php script. I am thinking that cronjob would work same as manually running script. Please give me idea.

My code is below.

#!/usr/local/bin/bash

DIR=/data/aa/bb
LOG=~account/HOME/log/dd
DATE=`date +%Y%m%d`
LOG_FILE=$LOG/move_files.$DATE.log
PROG=~account/HOME/bin/move_files.php

for type in "1" "2" "3" 
do
  echo "Check files in $DIR/dat/$type" >> $LOG_FILE
  $PROG $DIR/dat/$type $DIR/backup/$type >> $LOG_FILE

  echo "Compress files in $type" >> $LOG_FILE
  find $DIR/backup -name "*.DAT" -type f -exec gzip -f {} \;  >> $LOG_FILE
done

Upvotes: 0

Views: 92

Answers (4)

user347918
user347918

Reputation: 161

Thanks Guys, anyway i put on myscript following line:

export PATH=$PATH:/usr/local/bin

Then it's working now.

Thank you all of you guys.

Upvotes: 0

John
John

Reputation: 76

If you execute the script locally, you will visit to that folder (i.e. ~account/HOME/bin/)

By executing cron, it will use a new shell to execute the *.sh file in the present path. Therefore what you missing is to visit the path of where the .php/.sh file is contained. (absolute path is recommended)

cd ~account/HOME/bin/

The log file can be created because you issue an environment variable $LOG which indicates the exact location of that log file.

Upvotes: 0

gugabguerra
gugabguerra

Reputation: 683

Try calling the script specifying php binary path.

/path/to/php_binary script_file.php

E.g:

/usr/bin/php5 myscript.php argument1 argument2 >> mylogfile.log

Upvotes: 1

Saad Ismail
Saad Ismail

Reputation: 1440

I had the similar issue as you're doing. My backup script was working very well if it's called directly but wasn't working in crontab at all. I figured it out by adding this in the start (after shebang) of bash script:

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

Where "/opt" in the last is the directory where my bash script exists.

Upvotes: 1

Related Questions