31

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?

Guerlando OCs
  • 863
  • 10
  • 51
  • 88

4 Answers4

47

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.

dessert
  • 39,982
21

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 /etc/init.d/atd status or better still systemctl status atd)
  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'
    
  • 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 '19 at 09:52
  • 4
    @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 Feb 18 '19 at 12:56
  • 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 Feb 18 '19 at 14:44
  • 2
    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 Feb 20 '19 at 12:26
15

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
sudodus
  • 46,324
  • 5
  • 88
  • 152
  • 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 '19 at 08: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 '19 at 08: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 '19 at 10:10
  • Don't do this when $HOME or $USER variable is referenced in the script or any script/program that's sourced by the script. – M Imam Pratama Jun 19 '22 at 18:52
3

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)

Ludwik
  • 151
  • 4