update crontab file using php script - php

I have to create a crontab file using php script.This is my code :
<?php
file_put_contents('/tmp/crontab.txt','* * * * * NEW_CRON'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
?>
the file crontab.txt is created.But the next command 'crontab /tmp/crontab.txt' is not executed.When I type the command 'crontab -l',i get the output : 'no crontab for root'.But when I manually execute the command in terminal,the crontab is installed correctly.Why cant I execute the same command using my php script?

The server is not (should not be) running as root. Check the crontab for the HTTPd user (on debian/ubuntu running Apache, it's www-data).

Related

bash script running from command line but not from cron

I have a bash program that picks data from a file and delivers these data (if fulfilling a threshold) to another file.
It is a php script inside the bash script named smaoutput-analyse.sh
When executed from the shell it functions perfect.
When executed as a cron job as root is is executed correct, but there is no output.
Here is the output from grep -i cron /var/log/syslog
Aug 14 16:06:01 raspberrypi CRON[6705]: (root) CMD (/home/pi/scripts/SBFspot.sh > /home/pi/test/smaoutput.txt 2>&1 )
Aug 14 16:06:01 raspberrypi CRON[6706]: (root) CMD (/home/pi/test/smaoutput-analyse.sh > /dev/null 2>&1)
The information is (as mentioned before) correctly added when running fom the shell
#!/usr/bin/php
<?php
echo " Programm to read smaoutput.txt",PHP_EOL;
// etc etc
`if(!file_put_contents("sma_saved_data.txt",$sma_saved_data_string,FILE_APPEND)){
// failure
echo "error opening the file sma_saved_data.txt for writing",PHP_EOL;
}
// etc etc
?>
Here are the crontab lines:
# Every minute result of SMA
*/1 8-22 * * * /home/pi/scripts/SBFspot.sh > /home/pi/test/smaoutput.txt 2>&1
# afterwards read and save in file
*/1 10-20 * * * /home/pi/test/smaoutput-analyse.sh > /dev/null 2>&1
I think I have set file permissions correct +rw for the files and +rwx for the bash
What did I miss
You should check the following issues:
Are all the environment variables the same? So call printenv from bash and create a cron-job */1 8-22 * * * printenv > /tmp/printenv.txt --> Compare the ouput of file /tmp/printenv.txt with printenv from bash
Are you executing with the same user and the same permissions? Execute echo "$USER" and create a cron-job */1 8-22 * * * echo "$USER" > /tmp/user.txt --> Compare the ouput of file /tmp/user.txt with echo "$USER" from bash
Check the path you executing the script. Call pwd from bash and create a cron-job */1 8-22 * * * pwd> /tmp/pwd.txt --> Compare the ouput of file /tmp/pwd.txt with the ouput of pwd from bash
Realy interesting.
This was my first post and I received quite a swift response.
Thank you!
The solution was simple: provide the full path to where you want to keep the file.
Remark: To be shure that everything will be executed I alway put cronjobs in a root crontab and not in a user crontab.
Maybe that is "smart thinking" but not so smart acting.
I would appreciate to get some comment on this root cronjob idea.
The post about executing printenv, echo "&USER" and pwd both from bash and from cron was interesting.
The printenv from bash gives a lot of information, amongs which SHELL=/bin/bash, SSH_CLIENT, SSH_TTY. MAIL, PATH, SSH_CONNECTION and lots more starting with LS_COLORS, the printenv from cron is just 6 lines HOME=/root, LOGNAME=root, PATH=/usr/bin, LANG=en_GB,UTF-8, SHELL=/bin/sh and PWD=/root
The echo "&USER" from bash gives pi, whilst from cron gives a blank file
The pwd from bash gives /home/pi/test and from cron /root
These results are understandable.
Can I learn from it that I should create cronjobs as user pi and not as user root?

Execute php file from bash script as www-data using crontab

I am trying to run a php file every night at a certain time using crontab, however the php needs to be running as a www-data because of the directory permissions. To run it as www-data I am using the root crontab and changing the user in there, like so:
* 20 * * * sudo -u www-data /usr/bin/env TERM=xterm /path/to/dailyProc.sh
dailyProc is as follows
today=`date +"%d%m%y"`
year=`date +"%y"`
dm=`date +"%m%d"`
`tar -zxf /path/to/input/$today.tgz -C /path/to/output`
echo "starting data proc"
`/usr/bin/php5 -f /path/to/dataproc.php date=$dm year=$year`
echo "data proc done"
All other commands in dailyProc.sh work but the php doesnt run. The php is using an output buffer and writing it to a file, which works fine calling it from the command line but doesnt work when calling by cron.
I can also definitely run dailyProc.sh from the command line as www-data using
sudo -u www-data dailyProc.sh
and everything works as expected.
Is there any reason I would not be able to run this php file in dailyProc.sh using crontab when everything else in it works?
Cron can be run per user too.
crontab -u www-data -e
This works for me:
* 20 * * * su - www-data -C "/path/to/dailyProc.sh"
You do not need to use su or sudo in a crontab entry, because the 6th column is for the user name anyway. And you don't need to start a terminal, because you won't see it anyway. Hence, the following should do:
* 20 * * * www-data /path/to/dailyProc.sh
The Syntax error: word unexpected… you mentioned in a comment appears to be inside your code. Try running the script from the command line and start debugging from there.
To do this I used curl inside dailyProc.sh
today=`date +"%d%m%y"`
year=`date +"%y"`
dm=`date +"%m%d"`
`tar -zxf /path/to/input/$today.tgz -C /path/to/output`
echo "starting data proc"
`/usr/bin/curl "myserver.com/dataproc.php?date=$dm?year=$year"`
echo "data proc done"

cron running php file which uses shell_exec and redis-cli

I have a cronjob that runs the following:
* * * * * php /path/to/phpfile.php >> /cronlog.txt
when I run the php file in bash everything works, but when the cronjob runs it, one command fails:
shell_exec("redis-cli ping"); and returns an error that sh: 1: redis-cli: not found
Does anyone know why the cron user using PHP shell_exec would not be able to use the redis-cli command?
update
git diff /env_term.txt /env_cron.txt
-SHELL=/bin/bash
-TERM=screen
-SSH_CLIENT=*************
-SSH_TTY=/dev/pts/0
-USER=root
-LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.
-TERMCAP= { a bunch of giberish }
-PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
-MAIL=/var/mail/root
-STY=*************
-PWD=*************
-LANG=en_US.UTF-8
-HOME=/root
-SHLVL=2
LANGUAGE=en_US:en
+HOME=/root
LOGNAME=root
-WINDOW=2
-SSH_CONNECTION=*************
-LESSOPEN=| /usr/bin/lesspipe %s
-LESSCLOSE=/usr/bin/lesspipe %s %s
-_=/usr/bin/env
+PATH=/usr/bin:/bin
+LANG=en_US.UTF-8
+SHELL=/bin/sh
+PWD=*************
Did you check if your PATH variable is the same when cron is called.
A quick check is to add a dummy cron job to output the current environment variables passed to cron:
* * * * * env > /tmp/env.out
And then compare this output with when you run the env command from the terminal
try this solution:
cat cronjob
* * * * * php /path/to/phpfile.php >> /path/to/cronlog.txt
Then:
chmod +x cronjob
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
crontab cronjob
You can give absolute path of redis-cli to avoid any such issue related to environment variable.

system command run from console but not from crontab in linux

I have tried by writing the below command in php file and then run that php file from console as well as crontab.The code which i have in php file is like
#!/usr/bin/php -q
<?php
system ("/usr/local/sbin/googletts-cli.pl -o /var/lib/asterisk/sounds/asdapp/test.wav -s 0.9 -l en -t 'Some text is written here'");
?>
If i run this php file from consol then it is working fine and generate the wav file.But if i put that php file in crontab like
* * * * * /var/lib/asterisk/agi-bin/asdapp/jagu_test.php
Then it is not generating wav file.
I have also tried the solution which is given in below links:
Why is crontab not executing my PHP script?
PHP script works from command line, not from cron
I have tried with various way for run this php file from crontab but can not get any success.Can anybody know what is the exactly issue or any solution?
Commands executed from crontab and commandline have different environment variables, working path and user (if you don't specify otherwise).
Also, you are invoking PHP interpreter just to invoke a system command. It would be much easier to put this line in the crontab itself:
* * * * * /usr/local/sbin/googletts-cli.pl -o /var/lib/asterisk/sounds/asdapp/test.wav -s 0.9 -l en -t 'Some text is written here'

Crontab not functioning properly

I am trying to run a crontab on Ubuntu, I think I get the general idea of how to create a crontab
I did the following...
1) run command crontab -e
2) add entry 04 22 * * * /var/www/update_ranks >> /root/update_ranks.root.txt
3) check a text file was created under root/ named update_ranks.root.txt at the specified
time.
The file update_ranks.root.txt is empty and the php file is not executed, what am I doing wrong?
If update_ranks is a bash file try adding sh before the script sh /var/www/update_ranks
By the way, check if you are doing that as root user or user with writing rights to /root. Try sudo crontab -e.
EDIT:
If it's a PHP file, you need to execute it in php /usr/bin/php /var/www/update_ranks and if the file has extension, use this: /usr/bin/php /var/www/update_ranks.php

Categories