Run a command that requires sudo after a time has passedHow to allow execution without prompting for password...

Have any astronauts or cosmonauts died in space?

Do error bars on probabilities have any meaning?

Can you wish for more wishes from an Efreeti bound to service via an Efreeti Bottle?

Taking an academic pseudonym?

Does the kobold player race feature, Pack Tactics, give ranged attacks advantage?

What dissuades people from lying about where they live in order to reduce state income taxes?

Relation between roots and coefficients - manipulation of identities

I hate taking lectures, can I still survive in academia?

Why Third 'Reich'? Why is 'reich' not translated when 'third' is? What is the English synonym of reich?

The Late Queen Gives in to Remorse - Reverse Hangman

How can I differentiate duration vs starting time

Manager has noticed coworker's excessive breaks. Should I warn him?

Is Apex Sometimes Case Sensitive?

Is there a way to draw a level tree?

Which was the first story to feature space elevators?

How do I handle a blinded enemy which wants to attack someone it's sure is there?

How can I portray body horror and still be sensitive to people with disabilities?

Almost normal subgroup

Can "ee" appear in Latin?

What caused Doctor Strange to repent of his selfishness and become Earth's protector?

A semicolon (';') is not needed after a function declaration. C++

The Longest Chess Game

Can a planet be tidally unlocked?

How does the income of your target audience matter for logo design?



Run a command that requires sudo after a time has passed


How to allow execution without prompting for password using sudo?sudo: ruby: command not found after sshRun sudo command within directoryRun interpreted commands within sudoI changed the permission for /etc folder. Sudo is not working after thatHow to run `chmod` command form script with sudo permissions?How to run a command that requires sudo at loginHow to automatically show sudo prompt when command doesn't have enough permissionsRun sudo command with non-root user in Docker containerwriting a bash script that uses sudoSudo requires password after adding user to sudoers













26















I usually do



sleep 4h; command


to execute a command after 4h. However, if that command requires sudo, it'll not work.



Is it possible to give sudo permission at the moment I'm running the sleep command?










share|improve this question

























  • something like this unix.stackexchange.com/questions/391796/…

    – MatsK
    Feb 18 at 8:06











  • In that particular case I think you can just use the shutdown. command with sudo and the appropriate arguments to schedule sleep at a specific time.

    – oarfish
    yesterday











  • Related:  Pre-authorize sudo? (So it can be run later) and sudo in non-interactive script.

    – G-Man
    yesterday











  • What is the task you need to do, and why the exact 4 hour sleep?

    – Thorbjørn Ravn Andersen
    yesterday
















26















I usually do



sleep 4h; command


to execute a command after 4h. However, if that command requires sudo, it'll not work.



Is it possible to give sudo permission at the moment I'm running the sleep command?










share|improve this question

























  • something like this unix.stackexchange.com/questions/391796/…

    – MatsK
    Feb 18 at 8:06











  • In that particular case I think you can just use the shutdown. command with sudo and the appropriate arguments to schedule sleep at a specific time.

    – oarfish
    yesterday











  • Related:  Pre-authorize sudo? (So it can be run later) and sudo in non-interactive script.

    – G-Man
    yesterday











  • What is the task you need to do, and why the exact 4 hour sleep?

    – Thorbjørn Ravn Andersen
    yesterday














26












26








26


3






I usually do



sleep 4h; command


to execute a command after 4h. However, if that command requires sudo, it'll not work.



Is it possible to give sudo permission at the moment I'm running the sleep command?










share|improve this question
















I usually do



sleep 4h; command


to execute a command after 4h. However, if that command requires sudo, it'll not work.



Is it possible to give sudo permission at the moment I'm running the sleep command?







bash scripts sudo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Raghav Dinesh

55




55










asked Feb 18 at 7:55









Guerlando OCsGuerlando OCs

2501618




2501618













  • something like this unix.stackexchange.com/questions/391796/…

    – MatsK
    Feb 18 at 8:06











  • In that particular case I think you can just use the shutdown. command with sudo and the appropriate arguments to schedule sleep at a specific time.

    – oarfish
    yesterday











  • Related:  Pre-authorize sudo? (So it can be run later) and sudo in non-interactive script.

    – G-Man
    yesterday











  • What is the task you need to do, and why the exact 4 hour sleep?

    – Thorbjørn Ravn Andersen
    yesterday



















  • something like this unix.stackexchange.com/questions/391796/…

    – MatsK
    Feb 18 at 8:06











  • In that particular case I think you can just use the shutdown. command with sudo and the appropriate arguments to schedule sleep at a specific time.

    – oarfish
    yesterday











  • Related:  Pre-authorize sudo? (So it can be run later) and sudo in non-interactive script.

    – G-Man
    yesterday











  • What is the task you need to do, and why the exact 4 hour sleep?

    – Thorbjørn Ravn Andersen
    yesterday

















something like this unix.stackexchange.com/questions/391796/…

– MatsK
Feb 18 at 8:06





something like this unix.stackexchange.com/questions/391796/…

– MatsK
Feb 18 at 8:06













In that particular case I think you can just use the shutdown. command with sudo and the appropriate arguments to schedule sleep at a specific time.

– oarfish
yesterday





In that particular case I think you can just use the shutdown. command with sudo and the appropriate arguments to schedule sleep at a specific time.

– oarfish
yesterday













Related:  Pre-authorize sudo? (So it can be run later) and sudo in non-interactive script.

– G-Man
yesterday





Related:  Pre-authorize sudo? (So it can be run later) and sudo in non-interactive script.

– G-Man
yesterday













What is the task you need to do, and why the exact 4 hour sleep?

– Thorbjørn Ravn Andersen
yesterday





What is the task you need to do, and why the exact 4 hour sleep?

– Thorbjørn Ravn Andersen
yesterday










5 Answers
5






active

oldest

votes


















42














Use sudo to start a root shell where you run the commands:



sudo bash -c 'sleep 4h; command'


Every command running in the root shell runs with root permissions, which for sleep doesn’t hurt. If you need to run a command with user permissions in it use sudo -u USERNAME COMMAND, e.g.:



$ sudo bash -c 'sleep 4h; sudo -u dessert whoami; whoami'
dessert # whoami run as user dessert
root # whoami run as root




Another approach would be to use sudo visudo to allow the command’s execution without root access, see:
How to allow execution without prompting for password using sudo?

Note that depending on the command this may create a security flaw.






share|improve this answer

































    19














    Assuming you only want to run the process once (not, e.g. every 4 hours) then you can use atd




    1. Ensure that atd is running (in ubuntu that is normally /etc/init.d/atd status)


    2. At a terminal as root run your command as follows:



      # at now + 4 hours
      warning: commands will be executed using /bin/sh
      at> command
      at> CTRL-D



    3. If you want to run it every 4 hours you could also use cron (as root) with the following config in your crontab



      0 */4 * * * sh -c $'/path/to/command'







    share|improve this answer










    New contributor




    Ama Aje My Fren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.
















    • 2





      Yes, at is the right tool for this job, because it also takes care of I/O redirection, doesn't block a shell window, and works even when the user has logged out or the machine has been rebooted since.

      – Simon Richter
      Feb 18 at 9:52






    • 3





      @SimonRichter: sudo bash -c 'sleep 4h && command' & to put sudo in the background is an easier way to not block a shell window / tab. If you want the output to pop up asynchronously as a reminder that it happened, that's easier. It doesn't work across reboots, but depending on your nohup settings it might stay running after exiting / logging out from a shell.

      – Peter Cordes
      2 days ago






    • 4





      Note that at runs the command as soon as it’s able to when the system is suspended at the specified time, see here on U&L – depending on the command(s) to run this may not be what you want.

      – dessert
      2 days ago






    • 1





      Just a suggestion, you can also use systemctl to check the service status instead of directly calling that init.d script. While both seemingly have the same result, I think the modern way of interacting with systemd services should be preferred: systemctl status atd

      – Byte Commander
      22 hours ago



















    14














    One way is to run via a shellscript with sudo permissions (and give the password, when you start the shellscript), if the shellscript is in the current directory,



    sudo ./delayer 4h


    where delayer can be a shellscript with the content



    #!/bin/bash
    sleep "$1"
    command


    Make it executable with



    chmod +x delayer


    and copy or move it to a directory in PATH if you wish.





    If you want a more flexible shellscript, where you can select the command [line] to delay by entering parameter(s), you can try



    #!/bin/bash

    if [ $# -lt 2 ] || [ "$(whoami)" != "root" ]
    then
    echo "Delay start of command, that needs 'sudo'
    Usage: sudo $0 <delay> <command line>
    Example: sudo $0 4h parted -ls"
    exit
    fi

    sleep "$1"
    shift
    "$@"


    Demo example (short delay, 5s, for demo purpose),



    $ ./delayer
    Delay start of command, that needs 'sudo'
    Usage: sudo ./delayer <delay> <command line>
    Example: sudo ./delayer 4h parted -ls

    $ sudo ./delayer 5s parted /dev/sdc p
    [sudo] password for sudodus:
    Model: Kanguru SS3 (scsi)
    Disk /dev/sdc: 15,9GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:

    Number Start End Size File system Name Flags
    2 1049kB 2097kB 1049kB primary bios_grub
    3 2097kB 258MB 256MB fat32 primary boot, esp
    4 258MB 2274MB 2016MB primary
    5 2274MB 12,5GB 10,2GB ext2 primary
    1 12,5GB 15,9GB 3394MB ntfs primary msftdata





    share|improve this answer





















    • 3





      Well, if it's /bin/sh syntax it will be fine. But if you intend on using bash-specific features, then shebang is necessary. Me and steeldriver had discussion about that somewhere. Aaand Videonauth deleted his comment before I could respond properly. Oh well

      – Sergiy Kolodyazhnyy
      Feb 18 at 8:21













    • @SergiyKolodyazhnyy, You are right. So in this case the shebang is there to make the shellscript robust in case of added features where the syntax may differ.

      – sudodus
      Feb 18 at 8:27











    • @SergiyKolodyazhnyy Well it was just a suggestion so I thought leaving the comment there was not necessary and would only add to clutter. Feel free to hit me up in chat and point out your view :)

      – Videonauth
      Feb 18 at 10:10





















    2














    Another way would be to start sudo interactive session with sudo -s (does not change directory) or sudo -i (changes current directory to root home directory) and then enter your commands (without sudo)






    share|improve this answer








    New contributor




    Ludwik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.




























      1














      The best practice is



      sudo bash -c 'sleep 4h; command'


      That's all fit in your answer without any bash scripting or cron






      share|improve this answer










      New contributor




      Xander Mamet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
















      • 7





        Isn't this a duplicate of an existing answer?

        – Jeff Schaller
        yesterday











      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1119144%2frun-a-command-that-requires-sudo-after-a-time-has-passed%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      42














      Use sudo to start a root shell where you run the commands:



      sudo bash -c 'sleep 4h; command'


      Every command running in the root shell runs with root permissions, which for sleep doesn’t hurt. If you need to run a command with user permissions in it use sudo -u USERNAME COMMAND, e.g.:



      $ sudo bash -c 'sleep 4h; sudo -u dessert whoami; whoami'
      dessert # whoami run as user dessert
      root # whoami run as root




      Another approach would be to use sudo visudo to allow the command’s execution without root access, see:
      How to allow execution without prompting for password using sudo?

      Note that depending on the command this may create a security flaw.






      share|improve this answer






























        42














        Use sudo to start a root shell where you run the commands:



        sudo bash -c 'sleep 4h; command'


        Every command running in the root shell runs with root permissions, which for sleep doesn’t hurt. If you need to run a command with user permissions in it use sudo -u USERNAME COMMAND, e.g.:



        $ sudo bash -c 'sleep 4h; sudo -u dessert whoami; whoami'
        dessert # whoami run as user dessert
        root # whoami run as root




        Another approach would be to use sudo visudo to allow the command’s execution without root access, see:
        How to allow execution without prompting for password using sudo?

        Note that depending on the command this may create a security flaw.






        share|improve this answer




























          42












          42








          42







          Use sudo to start a root shell where you run the commands:



          sudo bash -c 'sleep 4h; command'


          Every command running in the root shell runs with root permissions, which for sleep doesn’t hurt. If you need to run a command with user permissions in it use sudo -u USERNAME COMMAND, e.g.:



          $ sudo bash -c 'sleep 4h; sudo -u dessert whoami; whoami'
          dessert # whoami run as user dessert
          root # whoami run as root




          Another approach would be to use sudo visudo to allow the command’s execution without root access, see:
          How to allow execution without prompting for password using sudo?

          Note that depending on the command this may create a security flaw.






          share|improve this answer















          Use sudo to start a root shell where you run the commands:



          sudo bash -c 'sleep 4h; command'


          Every command running in the root shell runs with root permissions, which for sleep doesn’t hurt. If you need to run a command with user permissions in it use sudo -u USERNAME COMMAND, e.g.:



          $ sudo bash -c 'sleep 4h; sudo -u dessert whoami; whoami'
          dessert # whoami run as user dessert
          root # whoami run as root




          Another approach would be to use sudo visudo to allow the command’s execution without root access, see:
          How to allow execution without prompting for password using sudo?

          Note that depending on the command this may create a security flaw.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 18 at 8:23

























          answered Feb 18 at 8:10









          dessertdessert

          24k670104




          24k670104

























              19














              Assuming you only want to run the process once (not, e.g. every 4 hours) then you can use atd




              1. Ensure that atd is running (in ubuntu that is normally /etc/init.d/atd status)


              2. At a terminal as root run your command as follows:



                # at now + 4 hours
                warning: commands will be executed using /bin/sh
                at> command
                at> CTRL-D



              3. If you want to run it every 4 hours you could also use cron (as root) with the following config in your crontab



                0 */4 * * * sh -c $'/path/to/command'







              share|improve this answer










              New contributor




              Ama Aje My Fren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.
















              • 2





                Yes, at is the right tool for this job, because it also takes care of I/O redirection, doesn't block a shell window, and works even when the user has logged out or the machine has been rebooted since.

                – Simon Richter
                Feb 18 at 9:52






              • 3





                @SimonRichter: sudo bash -c 'sleep 4h && command' & to put sudo in the background is an easier way to not block a shell window / tab. If you want the output to pop up asynchronously as a reminder that it happened, that's easier. It doesn't work across reboots, but depending on your nohup settings it might stay running after exiting / logging out from a shell.

                – Peter Cordes
                2 days ago






              • 4





                Note that at runs the command as soon as it’s able to when the system is suspended at the specified time, see here on U&L – depending on the command(s) to run this may not be what you want.

                – dessert
                2 days ago






              • 1





                Just a suggestion, you can also use systemctl to check the service status instead of directly calling that init.d script. While both seemingly have the same result, I think the modern way of interacting with systemd services should be preferred: systemctl status atd

                – Byte Commander
                22 hours ago
















              19














              Assuming you only want to run the process once (not, e.g. every 4 hours) then you can use atd




              1. Ensure that atd is running (in ubuntu that is normally /etc/init.d/atd status)


              2. At a terminal as root run your command as follows:



                # at now + 4 hours
                warning: commands will be executed using /bin/sh
                at> command
                at> CTRL-D



              3. If you want to run it every 4 hours you could also use cron (as root) with the following config in your crontab



                0 */4 * * * sh -c $'/path/to/command'







              share|improve this answer










              New contributor




              Ama Aje My Fren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.
















              • 2





                Yes, at is the right tool for this job, because it also takes care of I/O redirection, doesn't block a shell window, and works even when the user has logged out or the machine has been rebooted since.

                – Simon Richter
                Feb 18 at 9:52






              • 3





                @SimonRichter: sudo bash -c 'sleep 4h && command' & to put sudo in the background is an easier way to not block a shell window / tab. If you want the output to pop up asynchronously as a reminder that it happened, that's easier. It doesn't work across reboots, but depending on your nohup settings it might stay running after exiting / logging out from a shell.

                – Peter Cordes
                2 days ago






              • 4





                Note that at runs the command as soon as it’s able to when the system is suspended at the specified time, see here on U&L – depending on the command(s) to run this may not be what you want.

                – dessert
                2 days ago






              • 1





                Just a suggestion, you can also use systemctl to check the service status instead of directly calling that init.d script. While both seemingly have the same result, I think the modern way of interacting with systemd services should be preferred: systemctl status atd

                – Byte Commander
                22 hours ago














              19












              19








              19







              Assuming you only want to run the process once (not, e.g. every 4 hours) then you can use atd




              1. Ensure that atd is running (in ubuntu that is normally /etc/init.d/atd status)


              2. At a terminal as root run your command as follows:



                # at now + 4 hours
                warning: commands will be executed using /bin/sh
                at> command
                at> CTRL-D



              3. If you want to run it every 4 hours you could also use cron (as root) with the following config in your crontab



                0 */4 * * * sh -c $'/path/to/command'







              share|improve this answer










              New contributor




              Ama Aje My Fren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.










              Assuming you only want to run the process once (not, e.g. every 4 hours) then you can use atd




              1. Ensure that atd is running (in ubuntu that is normally /etc/init.d/atd status)


              2. At a terminal as root run your command as follows:



                # at now + 4 hours
                warning: commands will be executed using /bin/sh
                at> command
                at> CTRL-D



              3. If you want to run it every 4 hours you could also use cron (as root) with the following config in your crontab



                0 */4 * * * sh -c $'/path/to/command'








              share|improve this answer










              New contributor




              Ama Aje My Fren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              share|improve this answer



              share|improve this answer








              edited yesterday





















              New contributor




              Ama Aje My Fren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              answered Feb 18 at 9:06









              Ama Aje My FrenAma Aje My Fren

              1914




              1914




              New contributor




              Ama Aje My Fren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





              New contributor





              Ama Aje My Fren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              Ama Aje My Fren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.








              • 2





                Yes, at is the right tool for this job, because it also takes care of I/O redirection, doesn't block a shell window, and works even when the user has logged out or the machine has been rebooted since.

                – Simon Richter
                Feb 18 at 9:52






              • 3





                @SimonRichter: sudo bash -c 'sleep 4h && command' & to put sudo in the background is an easier way to not block a shell window / tab. If you want the output to pop up asynchronously as a reminder that it happened, that's easier. It doesn't work across reboots, but depending on your nohup settings it might stay running after exiting / logging out from a shell.

                – Peter Cordes
                2 days ago






              • 4





                Note that at runs the command as soon as it’s able to when the system is suspended at the specified time, see here on U&L – depending on the command(s) to run this may not be what you want.

                – dessert
                2 days ago






              • 1





                Just a suggestion, you can also use systemctl to check the service status instead of directly calling that init.d script. While both seemingly have the same result, I think the modern way of interacting with systemd services should be preferred: systemctl status atd

                – Byte Commander
                22 hours ago














              • 2





                Yes, at is the right tool for this job, because it also takes care of I/O redirection, doesn't block a shell window, and works even when the user has logged out or the machine has been rebooted since.

                – Simon Richter
                Feb 18 at 9:52






              • 3





                @SimonRichter: sudo bash -c 'sleep 4h && command' & to put sudo in the background is an easier way to not block a shell window / tab. If you want the output to pop up asynchronously as a reminder that it happened, that's easier. It doesn't work across reboots, but depending on your nohup settings it might stay running after exiting / logging out from a shell.

                – Peter Cordes
                2 days ago






              • 4





                Note that at runs the command as soon as it’s able to when the system is suspended at the specified time, see here on U&L – depending on the command(s) to run this may not be what you want.

                – dessert
                2 days ago






              • 1





                Just a suggestion, you can also use systemctl to check the service status instead of directly calling that init.d script. While both seemingly have the same result, I think the modern way of interacting with systemd services should be preferred: systemctl status atd

                – Byte Commander
                22 hours ago








              2




              2





              Yes, at is the right tool for this job, because it also takes care of I/O redirection, doesn't block a shell window, and works even when the user has logged out or the machine has been rebooted since.

              – Simon Richter
              Feb 18 at 9:52





              Yes, at is the right tool for this job, because it also takes care of I/O redirection, doesn't block a shell window, and works even when the user has logged out or the machine has been rebooted since.

              – Simon Richter
              Feb 18 at 9:52




              3




              3





              @SimonRichter: sudo bash -c 'sleep 4h && command' & to put sudo in the background is an easier way to not block a shell window / tab. If you want the output to pop up asynchronously as a reminder that it happened, that's easier. It doesn't work across reboots, but depending on your nohup settings it might stay running after exiting / logging out from a shell.

              – Peter Cordes
              2 days ago





              @SimonRichter: sudo bash -c 'sleep 4h && command' & to put sudo in the background is an easier way to not block a shell window / tab. If you want the output to pop up asynchronously as a reminder that it happened, that's easier. It doesn't work across reboots, but depending on your nohup settings it might stay running after exiting / logging out from a shell.

              – Peter Cordes
              2 days ago




              4




              4





              Note that at runs the command as soon as it’s able to when the system is suspended at the specified time, see here on U&L – depending on the command(s) to run this may not be what you want.

              – dessert
              2 days ago





              Note that at runs the command as soon as it’s able to when the system is suspended at the specified time, see here on U&L – depending on the command(s) to run this may not be what you want.

              – dessert
              2 days ago




              1




              1





              Just a suggestion, you can also use systemctl to check the service status instead of directly calling that init.d script. While both seemingly have the same result, I think the modern way of interacting with systemd services should be preferred: systemctl status atd

              – Byte Commander
              22 hours ago





              Just a suggestion, you can also use systemctl to check the service status instead of directly calling that init.d script. While both seemingly have the same result, I think the modern way of interacting with systemd services should be preferred: systemctl status atd

              – Byte Commander
              22 hours ago











              14














              One way is to run via a shellscript with sudo permissions (and give the password, when you start the shellscript), if the shellscript is in the current directory,



              sudo ./delayer 4h


              where delayer can be a shellscript with the content



              #!/bin/bash
              sleep "$1"
              command


              Make it executable with



              chmod +x delayer


              and copy or move it to a directory in PATH if you wish.





              If you want a more flexible shellscript, where you can select the command [line] to delay by entering parameter(s), you can try



              #!/bin/bash

              if [ $# -lt 2 ] || [ "$(whoami)" != "root" ]
              then
              echo "Delay start of command, that needs 'sudo'
              Usage: sudo $0 <delay> <command line>
              Example: sudo $0 4h parted -ls"
              exit
              fi

              sleep "$1"
              shift
              "$@"


              Demo example (short delay, 5s, for demo purpose),



              $ ./delayer
              Delay start of command, that needs 'sudo'
              Usage: sudo ./delayer <delay> <command line>
              Example: sudo ./delayer 4h parted -ls

              $ sudo ./delayer 5s parted /dev/sdc p
              [sudo] password for sudodus:
              Model: Kanguru SS3 (scsi)
              Disk /dev/sdc: 15,9GB
              Sector size (logical/physical): 512B/512B
              Partition Table: gpt
              Disk Flags:

              Number Start End Size File system Name Flags
              2 1049kB 2097kB 1049kB primary bios_grub
              3 2097kB 258MB 256MB fat32 primary boot, esp
              4 258MB 2274MB 2016MB primary
              5 2274MB 12,5GB 10,2GB ext2 primary
              1 12,5GB 15,9GB 3394MB ntfs primary msftdata





              share|improve this answer





















              • 3





                Well, if it's /bin/sh syntax it will be fine. But if you intend on using bash-specific features, then shebang is necessary. Me and steeldriver had discussion about that somewhere. Aaand Videonauth deleted his comment before I could respond properly. Oh well

                – Sergiy Kolodyazhnyy
                Feb 18 at 8:21













              • @SergiyKolodyazhnyy, You are right. So in this case the shebang is there to make the shellscript robust in case of added features where the syntax may differ.

                – sudodus
                Feb 18 at 8:27











              • @SergiyKolodyazhnyy Well it was just a suggestion so I thought leaving the comment there was not necessary and would only add to clutter. Feel free to hit me up in chat and point out your view :)

                – Videonauth
                Feb 18 at 10:10


















              14














              One way is to run via a shellscript with sudo permissions (and give the password, when you start the shellscript), if the shellscript is in the current directory,



              sudo ./delayer 4h


              where delayer can be a shellscript with the content



              #!/bin/bash
              sleep "$1"
              command


              Make it executable with



              chmod +x delayer


              and copy or move it to a directory in PATH if you wish.





              If you want a more flexible shellscript, where you can select the command [line] to delay by entering parameter(s), you can try



              #!/bin/bash

              if [ $# -lt 2 ] || [ "$(whoami)" != "root" ]
              then
              echo "Delay start of command, that needs 'sudo'
              Usage: sudo $0 <delay> <command line>
              Example: sudo $0 4h parted -ls"
              exit
              fi

              sleep "$1"
              shift
              "$@"


              Demo example (short delay, 5s, for demo purpose),



              $ ./delayer
              Delay start of command, that needs 'sudo'
              Usage: sudo ./delayer <delay> <command line>
              Example: sudo ./delayer 4h parted -ls

              $ sudo ./delayer 5s parted /dev/sdc p
              [sudo] password for sudodus:
              Model: Kanguru SS3 (scsi)
              Disk /dev/sdc: 15,9GB
              Sector size (logical/physical): 512B/512B
              Partition Table: gpt
              Disk Flags:

              Number Start End Size File system Name Flags
              2 1049kB 2097kB 1049kB primary bios_grub
              3 2097kB 258MB 256MB fat32 primary boot, esp
              4 258MB 2274MB 2016MB primary
              5 2274MB 12,5GB 10,2GB ext2 primary
              1 12,5GB 15,9GB 3394MB ntfs primary msftdata





              share|improve this answer





















              • 3





                Well, if it's /bin/sh syntax it will be fine. But if you intend on using bash-specific features, then shebang is necessary. Me and steeldriver had discussion about that somewhere. Aaand Videonauth deleted his comment before I could respond properly. Oh well

                – Sergiy Kolodyazhnyy
                Feb 18 at 8:21













              • @SergiyKolodyazhnyy, You are right. So in this case the shebang is there to make the shellscript robust in case of added features where the syntax may differ.

                – sudodus
                Feb 18 at 8:27











              • @SergiyKolodyazhnyy Well it was just a suggestion so I thought leaving the comment there was not necessary and would only add to clutter. Feel free to hit me up in chat and point out your view :)

                – Videonauth
                Feb 18 at 10:10
















              14












              14








              14







              One way is to run via a shellscript with sudo permissions (and give the password, when you start the shellscript), if the shellscript is in the current directory,



              sudo ./delayer 4h


              where delayer can be a shellscript with the content



              #!/bin/bash
              sleep "$1"
              command


              Make it executable with



              chmod +x delayer


              and copy or move it to a directory in PATH if you wish.





              If you want a more flexible shellscript, where you can select the command [line] to delay by entering parameter(s), you can try



              #!/bin/bash

              if [ $# -lt 2 ] || [ "$(whoami)" != "root" ]
              then
              echo "Delay start of command, that needs 'sudo'
              Usage: sudo $0 <delay> <command line>
              Example: sudo $0 4h parted -ls"
              exit
              fi

              sleep "$1"
              shift
              "$@"


              Demo example (short delay, 5s, for demo purpose),



              $ ./delayer
              Delay start of command, that needs 'sudo'
              Usage: sudo ./delayer <delay> <command line>
              Example: sudo ./delayer 4h parted -ls

              $ sudo ./delayer 5s parted /dev/sdc p
              [sudo] password for sudodus:
              Model: Kanguru SS3 (scsi)
              Disk /dev/sdc: 15,9GB
              Sector size (logical/physical): 512B/512B
              Partition Table: gpt
              Disk Flags:

              Number Start End Size File system Name Flags
              2 1049kB 2097kB 1049kB primary bios_grub
              3 2097kB 258MB 256MB fat32 primary boot, esp
              4 258MB 2274MB 2016MB primary
              5 2274MB 12,5GB 10,2GB ext2 primary
              1 12,5GB 15,9GB 3394MB ntfs primary msftdata





              share|improve this answer















              One way is to run via a shellscript with sudo permissions (and give the password, when you start the shellscript), if the shellscript is in the current directory,



              sudo ./delayer 4h


              where delayer can be a shellscript with the content



              #!/bin/bash
              sleep "$1"
              command


              Make it executable with



              chmod +x delayer


              and copy or move it to a directory in PATH if you wish.





              If you want a more flexible shellscript, where you can select the command [line] to delay by entering parameter(s), you can try



              #!/bin/bash

              if [ $# -lt 2 ] || [ "$(whoami)" != "root" ]
              then
              echo "Delay start of command, that needs 'sudo'
              Usage: sudo $0 <delay> <command line>
              Example: sudo $0 4h parted -ls"
              exit
              fi

              sleep "$1"
              shift
              "$@"


              Demo example (short delay, 5s, for demo purpose),



              $ ./delayer
              Delay start of command, that needs 'sudo'
              Usage: sudo ./delayer <delay> <command line>
              Example: sudo ./delayer 4h parted -ls

              $ sudo ./delayer 5s parted /dev/sdc p
              [sudo] password for sudodus:
              Model: Kanguru SS3 (scsi)
              Disk /dev/sdc: 15,9GB
              Sector size (logical/physical): 512B/512B
              Partition Table: gpt
              Disk Flags:

              Number Start End Size File system Name Flags
              2 1049kB 2097kB 1049kB primary bios_grub
              3 2097kB 258MB 256MB fat32 primary boot, esp
              4 258MB 2274MB 2016MB primary
              5 2274MB 12,5GB 10,2GB ext2 primary
              1 12,5GB 15,9GB 3394MB ntfs primary msftdata






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 days ago

























              answered Feb 18 at 8:09









              sudodussudodus

              24.8k32877




              24.8k32877








              • 3





                Well, if it's /bin/sh syntax it will be fine. But if you intend on using bash-specific features, then shebang is necessary. Me and steeldriver had discussion about that somewhere. Aaand Videonauth deleted his comment before I could respond properly. Oh well

                – Sergiy Kolodyazhnyy
                Feb 18 at 8:21













              • @SergiyKolodyazhnyy, You are right. So in this case the shebang is there to make the shellscript robust in case of added features where the syntax may differ.

                – sudodus
                Feb 18 at 8:27











              • @SergiyKolodyazhnyy Well it was just a suggestion so I thought leaving the comment there was not necessary and would only add to clutter. Feel free to hit me up in chat and point out your view :)

                – Videonauth
                Feb 18 at 10:10
















              • 3





                Well, if it's /bin/sh syntax it will be fine. But if you intend on using bash-specific features, then shebang is necessary. Me and steeldriver had discussion about that somewhere. Aaand Videonauth deleted his comment before I could respond properly. Oh well

                – Sergiy Kolodyazhnyy
                Feb 18 at 8:21













              • @SergiyKolodyazhnyy, You are right. So in this case the shebang is there to make the shellscript robust in case of added features where the syntax may differ.

                – sudodus
                Feb 18 at 8:27











              • @SergiyKolodyazhnyy Well it was just a suggestion so I thought leaving the comment there was not necessary and would only add to clutter. Feel free to hit me up in chat and point out your view :)

                – Videonauth
                Feb 18 at 10:10










              3




              3





              Well, if it's /bin/sh syntax it will be fine. But if you intend on using bash-specific features, then shebang is necessary. Me and steeldriver had discussion about that somewhere. Aaand Videonauth deleted his comment before I could respond properly. Oh well

              – Sergiy Kolodyazhnyy
              Feb 18 at 8:21







              Well, if it's /bin/sh syntax it will be fine. But if you intend on using bash-specific features, then shebang is necessary. Me and steeldriver had discussion about that somewhere. Aaand Videonauth deleted his comment before I could respond properly. Oh well

              – Sergiy Kolodyazhnyy
              Feb 18 at 8:21















              @SergiyKolodyazhnyy, You are right. So in this case the shebang is there to make the shellscript robust in case of added features where the syntax may differ.

              – sudodus
              Feb 18 at 8:27





              @SergiyKolodyazhnyy, You are right. So in this case the shebang is there to make the shellscript robust in case of added features where the syntax may differ.

              – sudodus
              Feb 18 at 8:27













              @SergiyKolodyazhnyy Well it was just a suggestion so I thought leaving the comment there was not necessary and would only add to clutter. Feel free to hit me up in chat and point out your view :)

              – Videonauth
              Feb 18 at 10:10







              @SergiyKolodyazhnyy Well it was just a suggestion so I thought leaving the comment there was not necessary and would only add to clutter. Feel free to hit me up in chat and point out your view :)

              – Videonauth
              Feb 18 at 10:10













              2














              Another way would be to start sudo interactive session with sudo -s (does not change directory) or sudo -i (changes current directory to root home directory) and then enter your commands (without sudo)






              share|improve this answer








              New contributor




              Ludwik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

























                2














                Another way would be to start sudo interactive session with sudo -s (does not change directory) or sudo -i (changes current directory to root home directory) and then enter your commands (without sudo)






                share|improve this answer








                New contributor




                Ludwik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.























                  2












                  2








                  2







                  Another way would be to start sudo interactive session with sudo -s (does not change directory) or sudo -i (changes current directory to root home directory) and then enter your commands (without sudo)






                  share|improve this answer








                  New contributor




                  Ludwik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  Another way would be to start sudo interactive session with sudo -s (does not change directory) or sudo -i (changes current directory to root home directory) and then enter your commands (without sudo)







                  share|improve this answer








                  New contributor




                  Ludwik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Ludwik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 2 days ago









                  LudwikLudwik

                  213




                  213




                  New contributor




                  Ludwik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Ludwik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Ludwik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.























                      1














                      The best practice is



                      sudo bash -c 'sleep 4h; command'


                      That's all fit in your answer without any bash scripting or cron






                      share|improve this answer










                      New contributor




                      Xander Mamet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.
















                      • 7





                        Isn't this a duplicate of an existing answer?

                        – Jeff Schaller
                        yesterday
















                      1














                      The best practice is



                      sudo bash -c 'sleep 4h; command'


                      That's all fit in your answer without any bash scripting or cron






                      share|improve this answer










                      New contributor




                      Xander Mamet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.
















                      • 7





                        Isn't this a duplicate of an existing answer?

                        – Jeff Schaller
                        yesterday














                      1












                      1








                      1







                      The best practice is



                      sudo bash -c 'sleep 4h; command'


                      That's all fit in your answer without any bash scripting or cron






                      share|improve this answer










                      New contributor




                      Xander Mamet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.










                      The best practice is



                      sudo bash -c 'sleep 4h; command'


                      That's all fit in your answer without any bash scripting or cron







                      share|improve this answer










                      New contributor




                      Xander Mamet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      share|improve this answer



                      share|improve this answer








                      edited 2 days ago









                      Dan

                      7,07934573




                      7,07934573






                      New contributor




                      Xander Mamet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      answered Feb 18 at 9:59









                      Xander MametXander Mamet

                      351




                      351




                      New contributor




                      Xander Mamet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





                      New contributor





                      Xander Mamet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






                      Xander Mamet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.








                      • 7





                        Isn't this a duplicate of an existing answer?

                        – Jeff Schaller
                        yesterday














                      • 7





                        Isn't this a duplicate of an existing answer?

                        – Jeff Schaller
                        yesterday








                      7




                      7





                      Isn't this a duplicate of an existing answer?

                      – Jeff Schaller
                      yesterday





                      Isn't this a duplicate of an existing answer?

                      – Jeff Schaller
                      yesterday


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1119144%2frun-a-command-that-requires-sudo-after-a-time-has-passed%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

                      Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

                      How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...