system command run from console but not from crontab in linux - php

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'

Related

Cron tab doesn't run most of tasks

I setup several cron jobs to make things work. laravel scheduler works perfectly but my other cronjobs not working at all.
*/2 * * * * /usr/bin/php /var/www/cronjobs/index.php
when I run on the console /usr/bin/php /var/www/cronjobs/index.php it works properly. I checked executable php path with which php and gives me /usr/bin/php nothing wrong with path afaik. I tried to run php script as apache user www-data I opened crontab with crontab -u www-data -e and paste command there.. it didn't work too.
I also tried send dummy notify with crontab and it also didn't work either
dummy example
* * * * * /usr/bin/notify-send 'test'
both of them doesn't work. What am I missing here ?
The second command will not send notification as cron have no idea of your desktop environment.
The first command probably use some environment variables. So instead of run in command line you can try to create a script:
#!/bin/bash
source /path/to/user/home/.bashrc #you can try also .bash_profile
/usr/bin/php /var/www/cronjobs/index.php
and your cron to be like:
*/2 * * * * /path/to/script.sh

Run PHP script in crontab only works when exporting to a file

On CentOS release 6.5 (Final):
I know that usually I just need to use the following command in crontab to run a php script.
0 * * * * /usr/local/bin/php absolute_path_file_to_the_script.php
But, recently, it stopped working. The only work around is to use the following command
0 * * * * /usr/local/bin/php absolute_path_file_to_the_script.php > log
But I would rather not to output anything to log for now.
So, I even tried
0 * * * * /bin/sh -c "/usr/local/bin/php absolute_path_file_to_the_script.php"
But the above commend is again not working in crontab (it works if I type in the shell directly).
And I am sure that the above command did run in crontab for a second with
ps ux, and then it stopped executing.
Any ideas on how to run the command properly without logging?
Try run that command (only php, without cron settings) from terminal and show result
Both answers from Marc and Greg work:
> /dev/null
or
> /dev/null 2>&1

How to start a remote PHP script via cronjob (alternative to wget)?

I need to start a remote PHP script (example.com/cron.php) every minute with a cronjob. At the moment, my cronjob looks like this: wget example.com/cron.php. This works, but puts a cron.php file on my server every time. How can I prevent this? Or are there alternatives to wget?
Quoting from manpage for wget:
-O file
Use of -O is not intended to mean simply "use the name file instead of the one in the URL;" rather, it is analogous to shell redirection: wget -O file http://foo is intended to work like wget -O - http://foo > file; file will be truncated immediately, and all downloaded content will be written there.
That means that -O - redirects output to stdout. And output on stdout you can simply redirect to /dev/null:
wget -O - http://example.com/cron.php >/dev/null
if your sever has lynx installed you could do lynx example.com/cron.php or you could use curl and do curl example.com/cron.php
This solution is for linux server:
To execute a cron job you need to have access to cronTab on the server:
to edit crontab use the commnad line :
sudo crontab -e
add a the script you would like to execute:
* * * * * php /path/to/your/script/cron.php 2>&1
Save your crontab and you should be done.
Please check the link http://en.wikipedia.org/wiki/Cron to understand the asterisk
Check out https://www.setcronjob.com/
This web program enables you to automatically schedule cron jobs on other servers.

Using CronTab to run php scripts

I need to send emails hourly and daily. I've tried nearly everything but it appears my crontab just won't work. If I run the scripts via a browser e.g
http://localhost/Maisha/Functions/sendhourlymails.php
my emails get sent beautifully.(I moved default website localhost to public_html.) I don't know whats wrong. I read some post in stack overflow including the executable path of php helps hence I've put the /usr/bin/php before the actual script to be cronned will work but it does not. Removing /usr/bin/php does not work. Adding php before the actual script isn't working.
I have the following entries in my crontab.
# m h dom mon dow command
0 * * * * /usr/bin/php /home/maxwell/public_html/Maisha/Functions/sendhourlymails.php
0 0 * * * /usr/bin/php /home/maxwell/public_html/Maisha/Functions/senddailymails.php
Try to call the script via http with wget like so:
* * * * * wget http://localhost/myscript >/dev/null 2>&1
Yeh, wget is good option, also you can try to use:
0 * * * * /usr/sbin/php /usr/bin/php /home/maxwell/public_html/Maisha/Functions/sendhourlymails.php
but it could work wrong due to relative paths.
Also you should look at http://php.net/manual/en/features.commandline.php
Try to put this into your .php file
<?php
#!/usr/local/bin/php -q
//your code here
?>
Then if you include any file into this file you must use something like:
include"/var/www/../your_absolute_path_from_root_folder/connect.php";
Finnaly make sure this file has the right permissions..Try
chmod 755 /var/www/.../file.php
Then if you edit your crontab file with the following command
vi /etc/crontab
put something like
10 6 * * * root php /var/www/..path../file.php
and restart the service with this command
/etc/init.d/cron restart
you have do your job!!
Note-Tip:the php file isn't neccessery to be into public_html
folder!!

Adding php script to cron

What should be given as the url to the script while adding it to cron scheduler. The script is at domain.com/scripts/script.php
PS:I am using cPanel
If you add the line
#!/usr/bin/php
to the beginning of your file (use 'which php' to find out your actual directory) and change the file mod to "executable", you should be able to run it just by calling like your second choice,
/public_html/scripts/script.php
I hope that works for you.
Here's a copy / paste out of one of the cron jobs that I run:
00 7 * * 1,2,3,4,5
/usr/local/bin/php
/home/processing/process.php
You must use the absolute path to the PHP binary as well as the absolute path to the script itself.
none of these.
but full absolute path from the root of the filesystem.
you can see that path with this code
echo __FILE__;
I had the habit of changing directory cd /var/www/vhosts/somesite.com/httdocs before running script with /usr/bin/php -f ./scriptname.php 2>&1 all in the same line on crontab.
I redirect the error output to get notified by email in case an execution error occured.
From crontab :
MAILTO=emailnotifications#mail.com
* * * * * cd /var/www/vhosts/domain.com/httpdocs/; /usr/bin/php -f testmail.php 2>&1

Categories