简体   繁体   中英

Crontab running issue

I have 500 php files i want to make a crontab to run them automatically (they store XML data in my database) my problem is :

when I make a crontab for each php file it works fine. the command like this:

* * * * * php /home/username/public_html/codes/php0.php

But when I want to run a shell script including all my php files like this :

* * * * * bash /home/username/public_html/codes/php.sh

it does not run.

php.sh:

#!/bin/sh

php php0.php
echo php0
php php1.php
echo php1
php php2.php
echo php2
.
.
.

Is it possible to wrap php files with bash script? and if yes why does not work am I miss something?

You are probably not running in the directory you believe you are.

So replace temporarily #!/bin/sh with #!/bin/sh -vx and add a

  pwd

at the beginning of your script (that is, the 2nd line)

Then perhaps add a

 cd /home/username/public_html/codes

or maybe define a variable and use it:

 mycodedir=/home/username/public_html/codes
 php $mycodedir/php0.php
 echo php0

etc...

I suggest to read the advanced bash scripting guide (even if it does have weaknesses).

At the moment, you have relative paths in your php.sh . The following line

php php0.php

means that php will look for the php0.php file in the current working directory. In a cron job, the working directory is usually / , ie the file system root. To check what the working directory really is, you can use the pwd command.

So your php.sh script looks for a file php0.php in your file system root, but the file is in /home/username/public_html/codes/ .

If you use absolute paths in your php.sh , the current working directory does not matter. So simply add absolute paths in your php.sh . The file will look like this:

#!/bin/sh

php /home/username/public_html/codes/php0.php
echo php0
php /home/username/public_html/codes/php1.php
echo php1
php /home/username/public_html/codes/php2.php
echo php2
.
.
.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM