0

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
5
  • first isn't it ~/account. check selinux logs if it is enforcing. Always use full path for files when running from cron job Commented Nov 11, 2015 at 5:04
  • @bansi ~account is valid, it means "the home directory of user 'account'". Commented Nov 11, 2015 at 7:07
  • @GeraldSchneider yes. I agree with you. Otherwise it shouldn't create log file. Commented Nov 11, 2015 at 7:13
  • I was just reconfirming. You need check your selinux logs. cron jobs have different permission than user shell. I had same problem even when the cron was running as root. Commented Nov 11, 2015 at 7:19
  • just try sealert -a on the audit log eg: sealert -a /var/log/audit/audit.log. You may need root permission for this. Commented Nov 11, 2015 at 7:23

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You mean like this. My script is under /home/account/HOME/bin #!/usr/local/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/account/HOME/bin DIR=/data/aa/bb LOG=~account/HOME/log/dd DATE=date +%Y%m%d
PATH is not required when using full paths in the script.
@user347918 yes this should work, for the next time surround the code with back-ticks.
1

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

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.