I have several scripts scheduled to run in my crontab, but can only see them as root (or using sudo). I need to have a PHP script (which is being run as nginx) be able to add a new line to the crontab file. To do this I created a shell script (owned by root) and gave the nginx user permission to sudo it via the /etc/sudoers file.
The last line of the /etc/sudoers file:
nginx ALL=NOPASSWD: /etc/nginx/addcron.sh
The PHP call to execute the script:
chdir("/etc/nginx/");
echo exec("2>&1 ./addcron.sh $custname", $output);
echo "<pre>".print_r($output, true)."</pre>";
My current crontab:
[ec2-user#ip-172-31-xx-xxx nginx]$ sudo crontab -l
* * * * * env > /tmp/env.output
* * * * * /usr/bin/php -f /var/www/html/example/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/demo/cron/cron.php
0 23 * * * rm /tmp/cachegrind.out.*
Meta information about my addcron.sh file:
[ec2-user#ip-172-31-xx-xxx nginx]$ pwd
/etc/nginx
[ec2-user#ip-172-31-xx-xxx nginx]$ ls -al addcron.sh
-rwxr-xr-x 1 root root 129 Nov 24 22:16 addcron.sh
The contents of addcron.sh:
#!/bin/bash
custname="$1"
(crontab -l; echo \"* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php\" ) | crontab -
When I try to run this though, I get the following error:
errors in crontab file, can't install.
Array
(
[0] => "-":102: bad minute
[1] => errors in crontab file, can't install.
)
It seems like it doesn't like the - mark in addcron.sh, but my Google searches suggest this is correct. Also, I have tried adding sudo to the PHP's exec command, but then I just get the following error:
sorry, you must have a tty to run sudo
What am I doing wrong or missing, and why?
Side note: running cron jobs unrelated to the system admin under the root's account is a BAD IDEA from the security prospective. Install your crons under the nginx user.
The issue is caused by the escaping of the quotes in your bash script (which you can check directly in bash, piece by piece, btw):
> (crontab -l; echo \"* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php\" )
no crontab for username
"* ... <all kinds of filenames in here> ... /usr/bin/php -f /var/www/html/the_customer/cron/cron.php"
Without the quote escaping things work a bit better:
> (crontab -l; echo "* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php" )
no crontab for username
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
You might want to add some protection against duplicating the entries, here's how crontab looks after a few invocations:
> crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:10 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:09 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:09 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:08 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:07 2015)
# (Cronie version 4.2)
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$custname= "someone";
$strCmd = "echo \"* * * * * /usr/bin/php -f /var/www/html/".$custname."\" >> /cron/cron.php";
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd($strCmd);
//assuming your distribution reloads / restarts using 'service'
$return2 = $shell->exeCmd('service crond restart');
this will append your line to the cron file.
Related
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");
I want to add a cron job in my Raspberry to execute a task every five minutes.
So I do in the terminal:
crontab -e
and then add at the file:
*/1 * * * * /usr/bin/php myscript path.
The script is something very simple. just to try if it works:
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = date("l jS \of F Y h:i:s A") . "<br>";;
fwrite($myfile, $txt);
fclose($myfile);
?>
The problem is that the date isn't updated, so the cron job doesn't work. Any idea of the problem ?
UPDATE
This what I got, when I execute crobtab -e
GNU nano 2.2.6 File: /tmp/crontab.3IXg0z/crontab
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * /usr/bin/php /full/path/myscript.php
First, make sure the script is executable:
chmod +x /path/to/some/script.php
Second, make sure your script has an appropriate #! (or "shebang") on the very first line:
#!/usr/bin/php
Then make sure your cron job is configured correctly. The format for cron is typically m h dom mon dow command
sudo crontab -e
*/5 * * * * /path/to/some/script.php
If you wanted to run a script every 5 minutes you should add this entry.
*/5 * * * * /usr/bin/php /full/path/to/php/script.php
Make sure you have PATH variable set correctly on crontab, so it can locate your file.
You can simple put following line in top of crontab
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/path/to/newfile.txt
Try, following test
* * * * * touch /tmp/hello
Do following to redirect result
*/5 * * * * /usr/bin/php /full/path/to/php/script.php > /tmp/out.txt
Make sure your script running on command line.
/usr/bin/php /full/path/to/php/script.php
Use the -f option to execute the script:
*/5 * * * * /usr/bin/php -f /full/path/to/php/script.php
Tail logs file to see its executing every 5 min
tail -f /var/log/cron
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;
?>
I am trying to execute a php script in conjunction with the Sleep command from Crontab. Here is the relevant code:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root HOME=/
# run-parts
* * * * * sleep 5 && root /usr/bin/php /path/to/the/php/file.php
But it won't execute at all. Any hints?
Remove root from sleep 5 && root /usr..
If you want to run it as root, the correct format is:
* * * * * root sleep 5 && /usr/bin/php /path/to/the/php/file.php
I am kinda newbie to this, I have a php script which I want to execute after every 1 hour, for this I updated the crontab file inside the /etc directory but I am not able to see whether its actually being called.
Here's the entry in my crontab file
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
*/5 * * * * /usr/bin/curl -o temp.txt http://myurl.com/postparser.php
I am not even able to see any temp.txt file getting generated
Can someone point me in the right direction
try running
*/5 * * * * /usr/bin/php /path/to/php/file/postparser.php
Change the /usr/bin/php part to your php executable path.
Not sure about this but, changing just the file would have no effect.
Try editing the tasks using the command crontab -e ("e" for editing).
If you want to edit the crontab for a certain user, use the -u parameter
for more, check the man:
http://linux.die.net/man/1/crontab
Good luck