Asked  7 Months ago    Answers:  5   Viewed   35 times

Is it possible to use PHP to create, edit and delete crontab jobs?

I know how to list the current crontab jobs of the Apache user:

$output = shell_exec('crontab -l');
echo $output;

But how to add a cron job with PHP? 'crontab -e' would just open a text editor and you will have to manually edit the entries before saving the file.

And how to delete a cron job with PHP? Again you have to manually do this by 'crontab -e'.

With a job string like this:

$job = '0 */2 * * * /usr/bin/php5 /home/user1/work.php';

How do I add it to the crontab jobs list with PHP?

 Answers

85

crontab command usage

usage:  crontab [-u user] file
        crontab [-u user] [ -e | -l | -r ]
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)

So,

$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');

The above can be used for both create and edit/append provided the user has the adequate file write permission.

To delete jobs:

echo exec('crontab -r');

Also, take note that apache is running as a particular user and that's usually not root, which means the cron jobs can only be changed for the apache user unless given crontab -u privilege to the apache user.

Wednesday, March 31, 2021
 
Zulakis
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
91

There are a lot of ways to solve this, one of them is by taggin the Edit and Delete labels or some other element ( td, tr or taking the value from the id field you already have). Here's a very simple (not the best but it can give a very clear idea of what the deal is with this things):

<td>
    <button id="edit_<?php echo $id ?>">Edit</button>
    <button id="delete_<?php echo $id ?>">Delete</button>
</td>

By this you can extrac the id value from button using Jquery o javascript.

Wednesday, March 31, 2021
 
Deyson
answered 7 Months ago
28

From the man page:

Commands are executed by cron(8) when the minute, hour, and month of year fields match the current time, and when at least one of the two day fields (day of month, or day of week) matches the current time (see ``Note'' below).

and later:

Note: The day of a command's execution can be specified by two fields -- day of month, and day of week. If both fields are restricted (ie, are not *), the command will be run when either field matches the current time. For example, ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

In other words, your script runs on the 1st through the 7th as well as any Sunday.

Wednesday, March 31, 2021
 
Viralk
answered 7 Months ago
17

I finally found out the answer: the $USER variable is empty, if I execute it from PHP. Now I use:

(crontab -l; echo "$0 $1 * * $2 /var/www/alarm.sh") | crontab -

and it works!

Wednesday, March 31, 2021
 
Guesser
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :