0

I need to run sudo php artisan backup:run and automatically enter password everyday 3pm. I know how to do this on Windows just by using tasks scheduler and set run as admin on the .bat script.

  • 1
    Only supported releases of Ubuntu (standard or public support) are on-topic for this site. Ubuntu 14.04 LTS is EOL (end-of-life) thus off-topic, and Ubuntu 14.04 ESM is in extended support and only supported by Canonical via Ubuntu Advantage thus also off-topic here. Refer https://askubuntu.com/help/on-topic https://help.ubuntu.com/community/EOLUpgrades https://fridge.ubuntu.com/2019/05/02/ubuntu-14-04-trusty-tahr-reached-end-of-life-on-april-25-2019-esm-available/ – guiverc Oct 08 '21 at 01:39

1 Answers1

1

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
   &quot;first-last&quot;.

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?

Greenonline
  • 2,081