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

Can a rabbi marry a couple in between the conception and birth of their child together, presuming they are fit for marriage?

Taking an academic pseudonym?

Does a star need to be inside a galaxy?

Why is the movie titled Gothika?

How to make clear what a part-humanoid character looks like when they're quite common in their world?

if else in jq is not giving expected output

Why is Shelob considered evil?

Unions, aliasing and type-punning in practice: what works and what does not?

What is the small cylinder on the firewall to the left of the battery?

Why did the ZX Spectrum use an internal speaker?

Why do climate experts from the UN/IPCC never mention Grand Solar Minimum?

"it is in run"? Ways to express a functioning policy

Why don't hotels offer ≥ 1 kitchen that must be booked?

How to properly launch tmux on terminal startup?

How can I learn to care less because it makes me sick?

Bacterial growth inhibitors used in Deodorants

Why are these receptacles so difficult to plug into?

Why, in A Midsummer Night's Dream, does "square" mean "quarrel"?

Is it illegal to infringe copyright if your boss or your client ordered you to do it?

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

Did President Obama tell President Trump he was close to starting a war with North Korea?

How to create a cover page like this?

How to run a binary file from crontab?

What happens when the last remaining players refuse to kill each other?



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













21















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
    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
    2 hours ago
















21















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
    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
    2 hours ago














21












21








21


2






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 yesterday









Raghav Dinesh

55




55










asked yesterday









Guerlando OCsGuerlando OCs

2251418




2251418













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

    – MatsK
    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
    2 hours ago



















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

    – MatsK
    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
    2 hours ago

















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

– MatsK
yesterday





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

– MatsK
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
2 hours ago





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
2 hours ago










5 Answers
5






active

oldest

votes


















35














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

































    15














    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
      yesterday






    • 1





      @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
      yesterday






    • 3





      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
      23 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
      yesterday













    • @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
      yesterday











    • @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
      yesterday





















    3














    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.





















    • Isn't this a duplicate of an existing answer?

      – Jeff Schaller
      52 mins ago



















    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.




















      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









      35














      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






























        35














        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




























          35












          35








          35







          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 yesterday

























          answered yesterday









          dessertdessert

          23.9k668104




          23.9k668104

























              15














              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
                yesterday






              • 1





                @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
                yesterday






              • 3





                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
                23 hours ago
















              15














              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
                yesterday






              • 1





                @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
                yesterday






              • 3





                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
                23 hours ago














              15












              15








              15







              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 yesterday









              Ama Aje My FrenAma Aje My Fren

              1514




              1514




              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
                yesterday






              • 1





                @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
                yesterday






              • 3





                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
                23 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
                yesterday






              • 1





                @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
                yesterday






              • 3





                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
                23 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
              yesterday





              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
              yesterday




              1




              1





              @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
              yesterday





              @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
              yesterday




              3




              3





              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
              23 hours 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
              23 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
                yesterday













              • @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
                yesterday











              • @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
                yesterday


















              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
                yesterday













              • @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
                yesterday











              • @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
                yesterday
















              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 22 hours ago

























              answered yesterday









              sudodussudodus

              24.7k32876




              24.7k32876








              • 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
                yesterday













              • @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
                yesterday











              • @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
                yesterday
















              • 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
                yesterday













              • @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
                yesterday











              • @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
                yesterday










              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
              yesterday







              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
              yesterday















              @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
              yesterday





              @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
              yesterday













              @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
              yesterday







              @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
              yesterday













              3














              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.





















              • Isn't this a duplicate of an existing answer?

                – Jeff Schaller
                52 mins ago
















              3














              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.





















              • Isn't this a duplicate of an existing answer?

                – Jeff Schaller
                52 mins ago














              3












              3








              3







              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 yesterday









              Dan

              7,06934573




              7,06934573






              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 yesterday









              Xander MametXander Mamet

              391




              391




              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.













              • Isn't this a duplicate of an existing answer?

                – Jeff Schaller
                52 mins ago



















              • Isn't this a duplicate of an existing answer?

                – Jeff Schaller
                52 mins ago

















              Isn't this a duplicate of an existing answer?

              – Jeff Schaller
              52 mins ago





              Isn't this a duplicate of an existing answer?

              – Jeff Schaller
              52 mins ago











              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 yesterday









                  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.






























                      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

                      Anexo:Material bélico de la Fuerza Aérea de Chile Índice Aeronaves Defensa...

                      Always On Availability groups resolving state after failover - Remote harden of transaction...

                      update json value to null Announcing the arrival of Valued Associate #679: Cesar Manara ...