php crontab -l showing different result from command line - php

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;
?>

Related

Cron job cannot open input file

I'm trying to create cron job on Mac. I set a script to execute every 1 minute. Every minute I get mail with:
"Could not open input file: /Users/andrejolesov/Documents/script.php".
String for cronjob -e is:
"* * * * * /usr/bin/php -q Users/andrejolesov/Documents/script.php"
I also tried:
"* * * * * /usr/bin/php -f /Users/andrejolesov/Documents/script.php"
P.S: script.php has 777 permissions. If I run the script in terminal it works.

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

Execute PHP in cron job on centos

I needed help on my cronjob experiment, im unable to accomplish what i wanted. below are the things I did.
Create a php file inside /var/www/html/bob/test/index.php
Set permission rights of the directory and file to executable (0777)
Specify a cronjob
My index.php
<?php
$myfile = fopen("testfile.txt", "w");
?>
My various tested parameters for crontab -e which didn't work
*/1 * * * * root /usr/bin/php /var/www/html/bob/test/index.php
*/1 * * * * root /var/www/html/bob/test/index.php
*/1 * * * * root php -q /var/www/html/bob/test/index.php
*/1 * * * * root /var/www/html/bob/test/index.php
looking at some of my /var/log/cron I dont see any error indication.
Sep 6 15:55:01 localhost CROND[4866]: (root) CMD (root /var/www/html/bob/test/index.php)
Sep 6 15:56:01 localhost CROND[4872]: (root) CMD (root /var/www/html/bob/test/index.php)
Sep 6 15:57:01 localhost CROND[4878]: (root) CMD (root /var/www/html/bob/test/index.php)
Lastly, when i run my php via #php /var/www/html/bob/test/index.php the code executes fine and creates that textfile.
You need to remove root from your cronjob. The log is showing it's trying to execute a program called root with /var/www/html/bob/test/index.php as a parameter.
Instead, use the following as your cronjob:
*/1 * * * * /usr/bin/php /var/www/html/bob/test/index.php
This answer states that if you want to set the user for a cronjob to be run, you have to edit /etc/crontab directly, not by using crontab -e.
1 * * * * /usr/bin/php /var/www/html/bob/test/index.php
if this not working, so you have a probleme of permission , try to use chown and chmod for giving the right permission
and for $myfile = fopen("testfile.txt", "w"); try to use the absolute path for the file testfile.txt like :
$myfile = fopen("/home/xxxx/testfile.txt", "w");

cron task not working in centos

I have an unknown problem like cron task is not running at all.. I have checked the cron status
# service crond status
crond (pid 30949) is running...
But task is not running, check my crontab list below
# crontab -l
* * * * * echo -e "\aBEEEP\n"
*/1 * * * * echo -e "\aBEEEP\n"
I added this, to check task is running or not. What is the issue, may be permission ??
No update in /var/log/cron
Thanks in advnace..
Where do you expect the echo output? Your terminal is not connected to this process.
to test if cron is working try, e.g.:
* * * * * touch /root/crontest
File will be created after 1 minute then.

Categories