Shell Script can be executed from terminal but not from the crontab - php

I have to export products in the .csv or .txt file automatically all the time for the price compare feeds.
First, I was able to export products only via URL like this:
https://site-name.com/Process/Cron.php?run=1&engine=ExportProducts&service=1&profile_id=90&export_file=testcron_export.csv
But, I needed to export the products automatically on some time interval and for that I found a way to run the URL for exports of products in .csv or .txt file from terminal with the following command:
REQUEST_URI=‘Process/Cron.php’ SCRIPT_NAME=‘Process/Cron.php’ php-cgi -f Process/Cron.php run=1 engine=ExportProducts service=1 id=90 export_file=testcron_export.csv
And the products were exported successfully.
But, when I am setting up this command in the crontab, it’s not executed.
I tried using different ways, one of the ways is:
Copied the command (from above) in bash script and executed it successfully from terminal, but when I set-up the bash script in the crontab it's not executed.
These are all the combinations that I’ve tried in the crontab:
*/2 * * * * REQUEST_URI=‘Process/Cron.php’ SCRIPT_NAME=‘Process/Cron.php’ php-cgi -f Process/Cron.php run=1 engine=ExportProducts service=1 id=90 export_file=testcron_export.csv
*/2 * * * * ./testcron2.sh
*/2 * * * * ~/www.site_name.com/testcron2.sh
*/2 * * * * sh /home/site_name/SspStore/testcron2.sh
*/2 * * * * sh testcron2.sh
*/2 * * * * /bin/sh testcron2.sh
*/2 * * * * /bin/sh /home/site_name/SspStore/testcron2.sh
*/2 * * * * php-cgi -f REQUEST_URI=‘Process/CoreCron.php’ SCRIPT_NAME=‘Process/Cron.php’ Process/Cron.php run=1 engine=ExportProducts service=1 id=90 export_file=testcron_export.csv
*/2 * * * * usr/bin/sh testcron2.sh
*/2 * * * * usr/bin/sh /home/site_name/SspStore/testcron2.sh
PS. The shell script (testcron2.sh) is in the home directory of the shop.
Maybe the problem is with php-cgi that cannot be executed from terminal or I am missing something?
Thank you very much in advance

Related

How to run CRON jobs on cPanel?

I am trying to set up CRON jobs on cPanel v. 76.0.18 and I get "no input file specified" (NIFS), and looked into other posts (e.g., NIFS1, NIFS 2, NIFS 3, NIFS 4), not sure how to solve it.
My samples of CRON jobs:
*/10 * * * * /usr/bin/php -q /home/dir/dir/cron_dir/file-1.php
0 * * * * /usr/bin/php -q /home/dir/dir/cron_dir/file-2.php
* * * * * /usr/bin/php -q /home/dir/dir/cron_dir/file-3.php
0 * * * * rm -rf /home/dir/dir/data_dir_1
0 0,6 * * * rm -rf /home/dir/dir/data_dir_2
0 0,4 * * * rm -rf /home/dir/dir/data_dir_3
How do I solve "no input file specified" error?
Thanks a million guys!
If the command executes correctly in CLI directly, you might try putting it in a .bash file and referencing that in cron. I've seen weird issues with commands in cron, and sometimes wrapping it in a script can help.
If it /doesn't/ execute correctly in CLI, then you might start there :p

crontab printing script instead of executing it

here my problem width crontab:
I need to run a php script everyday at noon and so on my crontab i set:
# 0 0 * * * /usr/bin/php -c /var/www/fanta-trade/operazioni_pianificate_medaglie.php > /var/www/cron_log
0 12 * * * root /usr/bin/php /var/www/offertegaseluce/operazioni_pianificate.php > /var/www/offertegaseluce/cron_log
*/15 * * * * /usr/bin/php -q /var/www/fanta-trade/operazioni_pianificate_ordini.php > /var/www/cron_log
#20 */6 * * * /root/certs/certbot-auto renew --quiet --no-self-upgrade
the line that i need is actually the second... i tried many debugging:
I checked the timezone and it is properly set to europe-rome
I checked width whereis the position of php and /usr/bin/php is correct
I tryed to run the script as root or just widthout user specification
0 12 * * * /usr/bin/php /var/www/offertegaseluce/operazioni_pianificate.php > /var/www/offertegaseluce/cron_log
I checked if crontab as a sevice is running and restarted it
I checked if the script itself is reacheable and if it works and if i call the script via browser it works
but actually crontab is not executing the script it just copy paste the cript itself to the log file
any suggestion?
thanks

Set path in /etc/crontab for php file

Hello I have to run cron which I have written in core php.
Path of my php file in
/var/www/html/xplore/crons/examples/example.php
when I go to path of mu file and run script using php example.php it give me proper output.
But How to give path of file in crontab I have tried it by using
*/2 * * * * root usr/bin/php xplore/crons/examples/example.php
Do you have any idea please help me in to that?
try like below,
*/2 * * * * /usr/bin/php -q /xplore/crons/examples/example.php
*/2 * * * * It means that cron will run in every 2 min.
Seems like the file path that you are passing is wrong. You can try this
*/2 * * * * root /usr/bin/php /xplore/crons/examples/example.php
if this path(xplore/crons/examples/example.php) is correct

Php crontab function call

I have written a crontab to call a function with following syntax to run every 5 minutes,
*/5 * * * * curl http://localhost/domain/path/front_orders/recursive_pay/F0C473D9BD583
in the function, I have redirected to google with redirect('http://www.google.com');for test but it does not seem to work.Any suggestions..
If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab.
*/5 * * * * lynx http://localhost/domain/path/front_orders/recursive_pay/F0C473D9BD583.php
or
*/5 * * * * /usr/bin/curl http://localhost/domain/path/front_orders/recursive_pay/F0C473D9BD583
or
*/5 * * * * /usr/bin/wget -q http://localhost/domain/path/front_orders/recursive_pay/F0C473D9BD583
instead of redirect('http://www.google.com'); trying using this:
header('Location: http://www.google.com');
Use curl or wget if you want to just "ping" a url.
*/5 * * * * /usr/bin/wget -O /dev/null http://localhost/domain/path/front_orders/recursive_pay/F0C473D9BD583
You can also use one of
/usr/bin/wget --spider http://localhost/... (if doing a HEAD call will do)
/usr/bin/curl http://localhost/... > dev/null 2>&1
If you use
/usr/bin/wget -q http://localhost/...
I think you will end up putting a copy of the result in whatever the 'current' directory is (differs based on which user's cron is running).

php crontab -l showing different result from command line

I created a file name 'testCrontab.php' in /var/www/html folder on amazon ec2, ubuntu-based,
$output = shell_exec('crontab -l');
var_dump($output);
The problem is when I invoked this file in browser it shows
string(207) "0 0,6,12,18 * * * /usr/bin/php /var/www/html/testExec.php 0 3 * * * /usr/bin/php /var/www/html/testCrawlback.php 0 4 * * * /usr/bin/php /var/www/html/testInsertCard.php * * * * * NEW_CRON * * * * * NEW_CRON "
Which I guessed those NEW_CRON are from the other time I tested inserting a new cron from php,
but when I invoked this file from command line by issuing /usr/bin/php /var/www/html/testCrontab.php it shows
string(169) "0 0,6,12,18 * * * /usr/bin/php /var/www/html/testExec.php
0 3 * * * /usr/bin/php /var/www/html/testCrawlback.php
0 4 * * * /usr/bin/php /var/www/html/testInsertCard.php"
Also, command crontab -l result in the latter output.
Please help enlighten me what happen here.
Result will be different because when you are executing script via browser the user will be www-data or nobody. It will show the crons of that user. If you execute the script from command line it will show the crons of that particular user.
Edit
Set cron for webuser
sudo crontab -u webuser -e
List crons of webuser
sudo crontab -u webuser -l
webuser will be nobody or www-data or apache
You can find it by executing the following code from web browser.
<?php $output = exec('whoami');
echo $output;
?>

Categories