简体   繁体   中英

Run sh file from php with cron

I have an sh file with file-removing commands.

I run it from php like this:

shell_exec("sudo -n ./truncatefiles.sh  2>&1");

Thats works fine if I open the PHP file from browser, but doesnt work from scheduled cron tab.

PHP user: www-data

If i run whoiami from cron, returns same: www-data

I added this to my visudo:

www-data ALL=(ALL) NOPASSWD: /www/sites/..../importscript/truncatefiles.sh

Shell exec for this sh file returns (from cron):

sudo: sorry, a password is required to run sudo

Why works it dirrefent way in cron?

What should I do for get it work?

PLease try to do the following, Try to log your output from crotab to a file, * * myscript.php >> /var/log/myjob.log 2>&1 This way you can debug your script. 1. Also the check the user and permissions for your shell script, php file. 2. try with sudo crotab -e

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