0

I want to setup a cronjob for PHP script in ubuntu

I enter this command in terminal

$ crontab -e

Then I choose nano editor which is recommended by ubuntu. Then I enter the blow line into that. Then I press control+C, it asking Y/N for save. I press Y and F2 for close.

* */2 * * * root php /var/www/html/script.php

Other things I've tried:

* */2 * * * /var/www/html/script.php
* */2 * * * root /var/www/html/script.php

After that, I restart cron using the below command.

sudo /etc/init.d/cron restart

Then I check crontab list using crontab -l, it says no cron job set for the root user.

I tried to directly create a crontab.txt file into the cron.hourly / cron.d directory with one of the above line.

I tried numerous forum and all says crontab -e then enter or create crontab file inside cron directory. Nothing is helping me. I am scratching my head.

What is the correct way to create cronjob for php script in ubuntu 16.04 & php version 7.0

8
  • Do you have a new line character at the end of your cronjob entry?? Commented Mar 14, 2018 at 12:40
  • no i dont have. Commented Mar 14, 2018 at 12:41
  • Add a newline character at the end of your cronjob entry and retry? :) Commented Mar 14, 2018 at 12:42
  • The first thing that you need to do is figure out what command you need to be running in the cron entry. You're showing three different things right now. Have you actually tried them directly on the command line to see which one (if any) works? Commented Mar 14, 2018 at 12:48
  • 1
    Do/did you get a crontab: installing new crontab after saving/exiting your crontab -e? Are you logged in as root or using a "regular" user? Commented Mar 14, 2018 at 12:48

2 Answers 2

2

Try like this to set crontab using root user,

sudo crontab -e

Do your changes via nano or vim. Finally save and quit

* */2 * * * /var/www/html/script.php
* */2 * * * root /var/www/html/script.php

No need to restart again using this sudo /etc/init.d/cron restart

Sign up to request clarification or add additional context in comments.

4 Comments

i want to set crone job to run at 3 am.
which timezone ?
set it like * 3 * * * root /var/www/html/script.php, see also crontab-generator.org
if i run only this command can set the cron job - * 3 * * * root /var/www/html/script.php
1

Try this one (as root user): 1. sudo crontab -e

* */2 * * * php -f /var/www/html/script.php > /dev/null 2>&1

OR

* */2 * * * cd /var/www/html/; php -f script.php > /dev/null 2>&1

for crontabs runing as www-data user use command sudo crontab -u www-data -e for editing

after save crontasks will be installed automaticaly.

OR You can create tmp_crontask_file with content * */2 * * * php -f /var/www/html/script.php > /dev/null 2>&1 AND next use sudo crontab tmp_crontask_file for install cron(s) from file (as root) sudo crontab -u www-data tmp_crontask_file (as www-data user).


Edit 1: WARNING! If you install cron from file (last option) content of file overwrite existing crontab.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.