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
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
Feb 18 at 8:06
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
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
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 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 theshutdown
. 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
add a comment |
something like this unix.stackexchange.com/questions/391796/…
– MatsK
Feb 18 at 8:06
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
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
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
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 yournohup
settings it might stay running after exiting / logging out from a shell.
– Peter Cordes
2 days ago
4
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
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
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
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
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 |
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
7
Isn't this a duplicate of an existing answer?
– Jeff Schaller
yesterday
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 Feb 18 at 8:23
answered Feb 18 at 8:10
dessertdessert
24k670104
24k670104
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
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 yournohup
settings it might stay running after exiting / logging out from a shell.
– Peter Cordes
2 days ago
4
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
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
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
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 yournohup
settings it might stay running after exiting / logging out from a shell.
– Peter Cordes
2 days ago
4
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
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
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 Feb 18 at 9:06
Ama Aje My FrenAma Aje My Fren
1914
1914
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
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 yournohup
settings it might stay running after exiting / logging out from a shell.
– Peter Cordes
2 days ago
4
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
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
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
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 yournohup
settings it might stay running after exiting / logging out from a shell.
– Peter Cordes
2 days ago
4
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
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
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
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
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
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
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 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
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
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
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 2 days ago
LudwikLudwik
213
213
New contributor
New contributor
add a comment |
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
7
Isn't this a duplicate of an existing answer?
– Jeff Schaller
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
7
Isn't this a duplicate of an existing answer?
– Jeff Schaller
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
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 2 days ago
Dan
7,07934573
7,07934573
New contributor
answered Feb 18 at 9:59
Xander MametXander Mamet
351
351
New contributor
New contributor
7
Isn't this a duplicate of an existing answer?
– Jeff Schaller
yesterday
add a comment |
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
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
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