Search code examples
ubuntucroncron-task

How do I write a cronjob?


So I've been looking up cronjobs for the past few minutes... I have a general sense of how to add one to my websites ubuntu system.... I need to make my system run a corn job once a minute (according to the software I'm having it use).

  1. First I log in via SSH..
  2. Then I enter root mode.
  3. Then I type crontab -e
  4. Then each line is a scheduled cron job....

The instructions on the softwares site says to just run the following command once a minute:

/usr/bin/php -d memory_limit=-1 -d max_execution_time=0 /home/mywebsite.com/215a/applications/core/interface/task/task.php 8222157ad26eg58q51dh343ha7j472az

I know that the start of the crontab line should read like this:

*/1 * * * * /path/to/command

My confusion is this.... Can I just put the instructed command in the /path/to/command part, or do I need to create a file and put the files address there? Also if I have to make a file, what format?


Solution

  • You may do it like so

    In the crontab

    */1 * * * * /usr/bin/php -d memory_limit=-1 -d max_execution_time=0 /home/mywebsite.com/215a/applications/core/interface/task/task.php 8222157ad26eg58q51dh343ha7j472az
    

    ==========================================================

    Alternatively, you may put the command in a shell script and execute the shell script.

    command.sh

    #!/bin/sh
    
    /usr/bin/php -d memory_limit=-1 -d max_execution_time=0 /home/mywebsite.com/215a/applications/core/interface/task/task.php 8222157ad26eg58q51dh343ha7j472az
    

    Make sure to make the shell script file executable

    $chmod a+x command.sh
    

    Then in the crontab

    */1 * * * * /path/to/command.sh
    

    ============OR=============

    Without making the shell script file executable

    In the crontab

    */1 * * * * /bin/sh /path/to/command.sh