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
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
add a comment |
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
something like this unix.stackexchange.com/questions/391796/…
– MatsK
yesterday
In that particular case I think you can just use theshutdown
. command with sudo and the appropriate arguments to schedule sleep at a specific time.
– oarfish
2 hours ago
add a comment |
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
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
bash scripts sudo
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 theshutdown
. command with sudo and the appropriate arguments to schedule sleep at a specific time.
– oarfish
2 hours ago
add a comment |
something like this unix.stackexchange.com/questions/391796/…
– MatsK
yesterday
In that particular case I think you can just use theshutdown
. 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
add a comment |
5 Answers
5
active
oldest
votes
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.
add a comment |
Assuming you only want to run the process once (not, e.g. every 4 hours) then you can use atd
- Ensure that atd is running (in ubuntu that is normally
/etc/init.d/atd status
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
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'
New contributor
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 yournohup
settings it might stay running after exiting / logging out from a shell.
– Peter Cordes
yesterday
3
Note thatat
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
add a comment |
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
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
add a comment |
The best practice is
sudo bash -c 'sleep 4h; command'
That's all fit in your answer without any bash scripting or cron
New contributor
Isn't this a duplicate of an existing answer?
– Jeff Schaller
52 mins ago
add a comment |
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)
New contributor
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited yesterday
answered yesterday
dessertdessert
23.9k668104
23.9k668104
add a comment |
add a comment |
Assuming you only want to run the process once (not, e.g. every 4 hours) then you can use atd
- Ensure that atd is running (in ubuntu that is normally
/etc/init.d/atd status
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
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'
New contributor
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 yournohup
settings it might stay running after exiting / logging out from a shell.
– Peter Cordes
yesterday
3
Note thatat
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
add a comment |
Assuming you only want to run the process once (not, e.g. every 4 hours) then you can use atd
- Ensure that atd is running (in ubuntu that is normally
/etc/init.d/atd status
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
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'
New contributor
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 yournohup
settings it might stay running after exiting / logging out from a shell.
– Peter Cordes
yesterday
3
Note thatat
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
add a comment |
Assuming you only want to run the process once (not, e.g. every 4 hours) then you can use atd
- Ensure that atd is running (in ubuntu that is normally
/etc/init.d/atd status
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
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'
New contributor
Assuming you only want to run the process once (not, e.g. every 4 hours) then you can use atd
- Ensure that atd is running (in ubuntu that is normally
/etc/init.d/atd status
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
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'
New contributor
edited yesterday
New contributor
answered yesterday
Ama Aje My FrenAma Aje My Fren
1514
1514
New contributor
New contributor
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 yournohup
settings it might stay running after exiting / logging out from a shell.
– Peter Cordes
yesterday
3
Note thatat
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
add a comment |
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 yournohup
settings it might stay running after exiting / logging out from a shell.
– Peter Cordes
yesterday
3
Note thatat
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
The best practice is
sudo bash -c 'sleep 4h; command'
That's all fit in your answer without any bash scripting or cron
New contributor
Isn't this a duplicate of an existing answer?
– Jeff Schaller
52 mins ago
add a comment |
The best practice is
sudo bash -c 'sleep 4h; command'
That's all fit in your answer without any bash scripting or cron
New contributor
Isn't this a duplicate of an existing answer?
– Jeff Schaller
52 mins ago
add a comment |
The best practice is
sudo bash -c 'sleep 4h; command'
That's all fit in your answer without any bash scripting or cron
New contributor
The best practice is
sudo bash -c 'sleep 4h; command'
That's all fit in your answer without any bash scripting or cron
New contributor
edited yesterday
Dan
7,06934573
7,06934573
New contributor
answered yesterday
Xander MametXander Mamet
391
391
New contributor
New contributor
Isn't this a duplicate of an existing answer?
– Jeff Schaller
52 mins ago
add a comment |
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
add a comment |
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)
New contributor
add a comment |
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)
New contributor
add a comment |
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)
New contributor
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)
New contributor
New contributor
answered yesterday
LudwikLudwik
213
213
New contributor
New contributor
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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