How to Set Cron Job Through PHP Script

How can I set cron job through PHP script

This will add a script that runs every day at 9:30am.

exec('echo -e "`crontab -l`\n30 9 * * * /path/to/script" | crontab -');

You may run into problems with permissions if you are running this script from a web server. To get around this, I would suggest a different approach.

Here is one possible solution. Create a list of scripts that need to be run. You can save this in a text file or in a database. Create a script to read this list and run it every minute or every 5 minutes (using a cronjob). Your script will need to be smart enough to decide when to run the list of scripts and when to simply exit.

Install a cron job with a php script

You just have to create the cron file, then use exec to set up that cron:

$cron_file = 'cron_filename';
// Create the file
touch($cron_file);
// Make it writable
chmod($cron_file, 0777);
// Save the cron
file_put_contents($cron_file, '* * * * * your_command');
// Install the cron
exec('crontab cron_file');

This requires that the user which PHP is run under has the right to make crontabs. This cron file will by default replace any other crons for that user, so make sure to ask the user if he wants to apply the cron. Also make sure the folder you're writing the crontab file in is writable.

Execute PHP script in cron job

Automated Tasks: Cron

Cron is a time-based scheduling service in Linux / Unix-like computer operating systems. Cron job are used to schedule commands to be executed periodically.
You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron./* directories. It also checks the /var/spool/cron/ directory.

Configuring Cron Tasks

In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:

*/10 * * * * /usr/bin/php /opt/test.php

In the above sample, the */10 * * * * represents when the task should happen. The first figure represents minutes – in this case, on every "ten" minute. The other figures represent, respectively, hour, day, month and day of the week.

* is a wildcard, meaning "every time".

Start with finding out your PHP binary by typing in command line:

whereis php

The output should be something like:

php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz

Specify correctly the full path in your command.

Type the following command to enter cronjob:

crontab -e

To see what you got in crontab.

EDIT 1:

To exit from vim editor without saving just click:

Shift+:

And then type q!

Setting up a Cron Job on CPanel to executes a PHP script

Since your script resides in your public_html directory you can use wget for your Cron Job

wget -O - -q https://yoursite.com/wallboard/update.php

-O - output is written to the standard output in this case it will go to the email address you specify in CPanel

-q quiet mode

How to run a PHP script using a Cron job

try

0/2 * * * * curl http://[your_id:port]/kyrill/filetest.php

Use PHP to create, edit and delete crontab jobs?

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.

Setting up Cron job using PHP script

Your first try got lots of problem:

  • The string has started with single quotation, ended with double.
  • variables inside strings that started with double quote will be parsed. so if you start your string with single quote, variable won't replace with their values.
  • you can not use single quote character within a string started with single quote, unless you escape with backslash. (same apply to double quote as well) example: $var = '\'test\''

So grammatically, your first try should be like this:

exec("echo -e \"`crontab -l`* * {$post['birthDay']} {$post['birthMonth']} * /home/jharvard/vhosts/pset7/public/{$post['lastname']}{$post['firstname']}.php\" | crontab -");

Better Solution

As I understand from your code, you want to do something on user's birthday.
A better solution is create one single cronjob manually, that will run one file every night, and that file will manage what to do (including who's birthday is that, sending them email, etc) instead of having a cronjob for every user.



Related Topics



Leave a reply



Submit