create cron job ( php ) & solution for some cron not working (php)

  • Posted on: 18 October 2016
  • By: jmu

Recently, I worked on cron jobs. I will give a simple tutorial on creating cron jobs and share some solution for my problem I suffered. For example: Cron jobs disappeared; how to list all cron jobs; why there are some cron do not work. 

Create Crons:

  1. Put your script files into whatever folder you want. But please remember the path for the script because we will use it later.
  2. Login your server in terminal.
  3. Run Crontab -e
  4. Write cron job command:
    1. Example for time schedule (I refered a generator below): 
      “Monday to Friday, every 30 mins between 7:00am and 7:00pm” 
      */30 7-19 * * 1-5
    2. Example for full cron command (sample script in php): 
      */30 7-19 * * 1-5 /usr/bin/php /home/usr/script/cron_sample.php
    3. Example for full cron command with printing log: 
      */30 7-19 * * 1-5 /usr/bin/php /home/usr/script/cron_sample.php > /home/usr/script/log/cron_sample.log
  5. Save and exit the Editor
  6. List all cron jobs => Run Crontab -l

Hints:

  1. If you want to delete all your cron jobs, run Crontab -r. Attention: This command will delete ALL your cron jobs. If you found your cron jobs are disappeared, you might type this command by mistake.
  2. If some of your cron jobs, which have already set, do not work, here are a CHECK LIST:
    1. Check if your script code works fine?
    2. Check if you save the cron setting sucessfully. (Run Crontab -l list all your jobs for checking / sudo crontab -l)
    3. Check if you have the permission to run the script file. (It's really painful to forget to check this)

References:

  1. KDE's Task Scheduler: I found this from an answer in askubuntu.com
  2. Crontab Guru: My favorite online tool for setting time schedule 
    Crontab generator
  3. Vim editor command: I used vim editor on my linux server.
  4. Detail tutorial for setting cron: I learnt how to set cron from here.

 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.