How to run scheduled task on Ubuntu with line from Laravel [closed]

This is should be pretty much the same on Ubuntu 14.04 as it is on the latest releases - however, they maybe some differences.

The task is divided into two parts:

  • To be able to sudo without a password, add the user to the sudoers file and allow them to not require a password.
  • To run a job automatically, use cron.

sudoers

Add the user to the sudo group with

usermod -aG sudo <username>

Change <username> to the user that you want to run the job as.

To avoid having to enter the password, edit the /etc/sudoers file:

visudo

and add the following line at the end of the file:

<username>  ALL=(ALL) NOPASSWD:ALL

Again, change <username> to the user that you want to run the job as.

Note that a better approach would be to create a new sudoers file for your particular user instead, instead of editing the main sudoers file, like so:

echo "<username>  ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/username

For more info see How to Add User to Sudoers in Ubuntu.

See also this question, How can I add a user as a new sudoer using the command line?

cron

cron is very powerful and there are a lot of different options. However, to accomplish your task, here are just the basic essentials:

To edit the crontab, use

crontab -e

or for a different user, use

crontab -u ostechnix -e

If you have never run crontab before, you may be asked to choose an editor the first time that you run it.

Then for a 3 pm job add the line

0 15 * * * <command-to-execute>

So in your case use

0 15 * * * sudo php artisan backup:run

Save and exit to editor. Then to check the crontab, use

crontab -l

If you want to change the time the fields are as follows, from the cron man page.

       The time and date fields are:

              field          allowed values
              -----          --------------
              minute         0-59
              hour           0-23
              day of month   1-31
              month          1-12 (or names, see below)
              day of week    0-7 (0 or 7 is Sunday, or use names)

       A field may contain an asterisk (*), which always stands for
       "first-last".

For more info about crontab, see A Beginners Guide To Cron Jobs, or type man cron.

See also this question, How do I set up a Cron job?