Results 1 to 4 of 4

Thread: sutdown command in executable script run by crontab

  1. #1
    Join Date
    Mar 2006
    Location
    Slipery Slope, Earth
    Beans
    708
    Distro
    Ubuntu 20.04 Focal Fossa

    sutdown command in executable script run by crontab

    Hello Ubuntuers:

    I wanto to run an executable script at 4am every day in the day using crontab.... and the only command I want in that script is the shutdown command.

    My executable script to do this does not work. Here it is:


    Also, here is my crontab command:

    00 04 * * * /path/shutdown.sh > /path/shutdown.log 2>&1
    I have checked and my script is executable: -rwxrwxrwx

    This scheme does not work. The documentations says that shutdown can only be run by sudo.

    How do I get it to run as sudo in an executable script run by crontab?

    Thanks,
    Old Jimma

  2. #2
    Join Date
    Jun 2014
    Beans
    6,154

    Re: sutdown command in executable script run by crontab

    You have your thread marked as solved, is it? If not, one way to do this is to preface the shutdown command with sudo:

    00 04 * * * sudo /path/shutdown.sh > /path/shutdown.log 2>&1
    You can then use visduo to give your user privilege to run that specific command as root (sudo). Example at the link below.

    https://www.atrixnet.com/allow-an-un...and-with-sudo/

  3. #3
    Join Date
    Dec 2014
    Beans
    1,741

    Re: sutdown command in executable script run by crontab

    Since shutdown is supposed to be run by root (or someone allowed to act as root through sudo), I see two ways - or three if we count yancek's idea of changing /etc/sudoers allowing your account to run 'sudo shutdown -r' without asking for a password - to do this. One would be to put the command into root's crontab ('sudo crontab -u root -e'). There should be no need for sudo then. The other would be to put the command into the system crontab (/etc/crontab or a file in /etc/cron.d) with the user account field set to 'root'. See the manual page for the crontab file format for details (man 5 crontab), especially the differences in format between a normal user-specific crontab and the system crontab.

    Holger
    Last edited by Holger_Gehrke; March 13th, 2021 at 09:03 PM.

  4. #4
    Join Date
    Mar 2006
    Location
    Slipery Slope, Earth
    Beans
    708
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: sutdown command in executable script run by crontab

    Dear Holger and Yancek:

    THank you for your replies.

    I'll start working on this. I've gotta gig and have to split. Be back tomorrow!

    Old

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •