How do I start and stop a systemctl service inside a bash script?
I am writing a bash script to perform a daily backup. This script will ultimately run automatically every morning (cron or systemd).
What I would like to accomplish is
- Stop myservice
- Perform backup procedures
- Start myservice
The bash script I have created looks something like this:
# Stop myservice
systemctl stop myservice.service
# Do all the backing up here...
# Start myservice
systemctl start myservice.service
The issue I am having is that when I run this script, it requires my password during the systemctl stop/start calls. If this is to run automatically, obviously it can't require a password every time. How do I run this script automatically without requiring this password?
Ubuntu 18.04
Thanks!
command-line 18.04 bash backup
add a comment |
I am writing a bash script to perform a daily backup. This script will ultimately run automatically every morning (cron or systemd).
What I would like to accomplish is
- Stop myservice
- Perform backup procedures
- Start myservice
The bash script I have created looks something like this:
# Stop myservice
systemctl stop myservice.service
# Do all the backing up here...
# Start myservice
systemctl start myservice.service
The issue I am having is that when I run this script, it requires my password during the systemctl stop/start calls. If this is to run automatically, obviously it can't require a password every time. How do I run this script automatically without requiring this password?
Ubuntu 18.04
Thanks!
command-line 18.04 bash backup
add a comment |
I am writing a bash script to perform a daily backup. This script will ultimately run automatically every morning (cron or systemd).
What I would like to accomplish is
- Stop myservice
- Perform backup procedures
- Start myservice
The bash script I have created looks something like this:
# Stop myservice
systemctl stop myservice.service
# Do all the backing up here...
# Start myservice
systemctl start myservice.service
The issue I am having is that when I run this script, it requires my password during the systemctl stop/start calls. If this is to run automatically, obviously it can't require a password every time. How do I run this script automatically without requiring this password?
Ubuntu 18.04
Thanks!
command-line 18.04 bash backup
I am writing a bash script to perform a daily backup. This script will ultimately run automatically every morning (cron or systemd).
What I would like to accomplish is
- Stop myservice
- Perform backup procedures
- Start myservice
The bash script I have created looks something like this:
# Stop myservice
systemctl stop myservice.service
# Do all the backing up here...
# Start myservice
systemctl start myservice.service
The issue I am having is that when I run this script, it requires my password during the systemctl stop/start calls. If this is to run automatically, obviously it can't require a password every time. How do I run this script automatically without requiring this password?
Ubuntu 18.04
Thanks!
command-line 18.04 bash backup
command-line 18.04 bash backup
asked Dec 21 '18 at 16:41
noslenkwahnoslenkwah
1183
1183
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You have multiple possibilities, depending on your needs and preferences.
An apparent approach …
… would be to run the whole script as user root
by adding it to root
's crontab
(using sudo crontab -e
). It won't need any password then when systemctl stop/start myservice.service
is run. The downside is that you may need to run the backup tasks as another user (say noslenkwah
) and have to switch to that other user for the backup. Example:
# Stop myservice
systemctl stop myservice.service
# Do all the backing up here...
# ... and run the backup_command as user "otheruser":
sudo -u noslenkwah /path/to/backup_command --with --some --options
# Start myservice
systemctl start myservice.service
Another approach …
… would be to add the systemctl
commands to a file in the /etc/sudoers.d
directory so that a specific user may run them without supplying a password.
issue
sudo visudo -f /etc/sudoers.d/noslenkwah
(The filename,noslenkwah
doesn't matter, it is just a
personal habit of mine to name the files after the "main"
user affected by the settings in that file. It just needs
to be a file below the directory/etc/sudoers.d
.)
Add the following lines and save the file.
Cmnd_Alias MYSERVICE =
/bin/systemctl stop myservice.service,
/bin/systemctl start myservice.service
noslenkwah ALL = (root) NOPASSWD: MYSERVICE
This allows the user noslenkwah
to run sudo systemctl stop myservice.service
and sudo systemctl start myservice.service
without a password. It defines a socalled command alias (collection of commands) named MYSERVICE
and then allows
- the user
noslenkwah
- on
ALL
computers - as user
root
- without a password
- to run the commands defined by
MYSERVICE
Replace noslenkwah
and myservice
with the actual username and service name. Note that you really must issue sudo systemctl start myservice.service
for this to work (not sudo systemctl start myservice
(without .service
, for example).
Don't care about the "on ALL
computers" part. This is relevant only if you intend to distribute the very same sudoers
file to multiple computers.
You would then change your backup script to
# Stop myservice
sudo systemctl stop myservice.service
# Do all the backing up here...
/path/to/backup_command --with --some --options
# Start myservice
sudo systemctl start myservice.service
and have it run as user noslenkwah
.
add a comment |
Run this shell script as root.
For example, put it in the root crontab:
sudo crontab -e
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%2f1103632%2fhow-do-i-start-and-stop-a-systemctl-service-inside-a-bash-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have multiple possibilities, depending on your needs and preferences.
An apparent approach …
… would be to run the whole script as user root
by adding it to root
's crontab
(using sudo crontab -e
). It won't need any password then when systemctl stop/start myservice.service
is run. The downside is that you may need to run the backup tasks as another user (say noslenkwah
) and have to switch to that other user for the backup. Example:
# Stop myservice
systemctl stop myservice.service
# Do all the backing up here...
# ... and run the backup_command as user "otheruser":
sudo -u noslenkwah /path/to/backup_command --with --some --options
# Start myservice
systemctl start myservice.service
Another approach …
… would be to add the systemctl
commands to a file in the /etc/sudoers.d
directory so that a specific user may run them without supplying a password.
issue
sudo visudo -f /etc/sudoers.d/noslenkwah
(The filename,noslenkwah
doesn't matter, it is just a
personal habit of mine to name the files after the "main"
user affected by the settings in that file. It just needs
to be a file below the directory/etc/sudoers.d
.)
Add the following lines and save the file.
Cmnd_Alias MYSERVICE =
/bin/systemctl stop myservice.service,
/bin/systemctl start myservice.service
noslenkwah ALL = (root) NOPASSWD: MYSERVICE
This allows the user noslenkwah
to run sudo systemctl stop myservice.service
and sudo systemctl start myservice.service
without a password. It defines a socalled command alias (collection of commands) named MYSERVICE
and then allows
- the user
noslenkwah
- on
ALL
computers - as user
root
- without a password
- to run the commands defined by
MYSERVICE
Replace noslenkwah
and myservice
with the actual username and service name. Note that you really must issue sudo systemctl start myservice.service
for this to work (not sudo systemctl start myservice
(without .service
, for example).
Don't care about the "on ALL
computers" part. This is relevant only if you intend to distribute the very same sudoers
file to multiple computers.
You would then change your backup script to
# Stop myservice
sudo systemctl stop myservice.service
# Do all the backing up here...
/path/to/backup_command --with --some --options
# Start myservice
sudo systemctl start myservice.service
and have it run as user noslenkwah
.
add a comment |
You have multiple possibilities, depending on your needs and preferences.
An apparent approach …
… would be to run the whole script as user root
by adding it to root
's crontab
(using sudo crontab -e
). It won't need any password then when systemctl stop/start myservice.service
is run. The downside is that you may need to run the backup tasks as another user (say noslenkwah
) and have to switch to that other user for the backup. Example:
# Stop myservice
systemctl stop myservice.service
# Do all the backing up here...
# ... and run the backup_command as user "otheruser":
sudo -u noslenkwah /path/to/backup_command --with --some --options
# Start myservice
systemctl start myservice.service
Another approach …
… would be to add the systemctl
commands to a file in the /etc/sudoers.d
directory so that a specific user may run them without supplying a password.
issue
sudo visudo -f /etc/sudoers.d/noslenkwah
(The filename,noslenkwah
doesn't matter, it is just a
personal habit of mine to name the files after the "main"
user affected by the settings in that file. It just needs
to be a file below the directory/etc/sudoers.d
.)
Add the following lines and save the file.
Cmnd_Alias MYSERVICE =
/bin/systemctl stop myservice.service,
/bin/systemctl start myservice.service
noslenkwah ALL = (root) NOPASSWD: MYSERVICE
This allows the user noslenkwah
to run sudo systemctl stop myservice.service
and sudo systemctl start myservice.service
without a password. It defines a socalled command alias (collection of commands) named MYSERVICE
and then allows
- the user
noslenkwah
- on
ALL
computers - as user
root
- without a password
- to run the commands defined by
MYSERVICE
Replace noslenkwah
and myservice
with the actual username and service name. Note that you really must issue sudo systemctl start myservice.service
for this to work (not sudo systemctl start myservice
(without .service
, for example).
Don't care about the "on ALL
computers" part. This is relevant only if you intend to distribute the very same sudoers
file to multiple computers.
You would then change your backup script to
# Stop myservice
sudo systemctl stop myservice.service
# Do all the backing up here...
/path/to/backup_command --with --some --options
# Start myservice
sudo systemctl start myservice.service
and have it run as user noslenkwah
.
add a comment |
You have multiple possibilities, depending on your needs and preferences.
An apparent approach …
… would be to run the whole script as user root
by adding it to root
's crontab
(using sudo crontab -e
). It won't need any password then when systemctl stop/start myservice.service
is run. The downside is that you may need to run the backup tasks as another user (say noslenkwah
) and have to switch to that other user for the backup. Example:
# Stop myservice
systemctl stop myservice.service
# Do all the backing up here...
# ... and run the backup_command as user "otheruser":
sudo -u noslenkwah /path/to/backup_command --with --some --options
# Start myservice
systemctl start myservice.service
Another approach …
… would be to add the systemctl
commands to a file in the /etc/sudoers.d
directory so that a specific user may run them without supplying a password.
issue
sudo visudo -f /etc/sudoers.d/noslenkwah
(The filename,noslenkwah
doesn't matter, it is just a
personal habit of mine to name the files after the "main"
user affected by the settings in that file. It just needs
to be a file below the directory/etc/sudoers.d
.)
Add the following lines and save the file.
Cmnd_Alias MYSERVICE =
/bin/systemctl stop myservice.service,
/bin/systemctl start myservice.service
noslenkwah ALL = (root) NOPASSWD: MYSERVICE
This allows the user noslenkwah
to run sudo systemctl stop myservice.service
and sudo systemctl start myservice.service
without a password. It defines a socalled command alias (collection of commands) named MYSERVICE
and then allows
- the user
noslenkwah
- on
ALL
computers - as user
root
- without a password
- to run the commands defined by
MYSERVICE
Replace noslenkwah
and myservice
with the actual username and service name. Note that you really must issue sudo systemctl start myservice.service
for this to work (not sudo systemctl start myservice
(without .service
, for example).
Don't care about the "on ALL
computers" part. This is relevant only if you intend to distribute the very same sudoers
file to multiple computers.
You would then change your backup script to
# Stop myservice
sudo systemctl stop myservice.service
# Do all the backing up here...
/path/to/backup_command --with --some --options
# Start myservice
sudo systemctl start myservice.service
and have it run as user noslenkwah
.
You have multiple possibilities, depending on your needs and preferences.
An apparent approach …
… would be to run the whole script as user root
by adding it to root
's crontab
(using sudo crontab -e
). It won't need any password then when systemctl stop/start myservice.service
is run. The downside is that you may need to run the backup tasks as another user (say noslenkwah
) and have to switch to that other user for the backup. Example:
# Stop myservice
systemctl stop myservice.service
# Do all the backing up here...
# ... and run the backup_command as user "otheruser":
sudo -u noslenkwah /path/to/backup_command --with --some --options
# Start myservice
systemctl start myservice.service
Another approach …
… would be to add the systemctl
commands to a file in the /etc/sudoers.d
directory so that a specific user may run them without supplying a password.
issue
sudo visudo -f /etc/sudoers.d/noslenkwah
(The filename,noslenkwah
doesn't matter, it is just a
personal habit of mine to name the files after the "main"
user affected by the settings in that file. It just needs
to be a file below the directory/etc/sudoers.d
.)
Add the following lines and save the file.
Cmnd_Alias MYSERVICE =
/bin/systemctl stop myservice.service,
/bin/systemctl start myservice.service
noslenkwah ALL = (root) NOPASSWD: MYSERVICE
This allows the user noslenkwah
to run sudo systemctl stop myservice.service
and sudo systemctl start myservice.service
without a password. It defines a socalled command alias (collection of commands) named MYSERVICE
and then allows
- the user
noslenkwah
- on
ALL
computers - as user
root
- without a password
- to run the commands defined by
MYSERVICE
Replace noslenkwah
and myservice
with the actual username and service name. Note that you really must issue sudo systemctl start myservice.service
for this to work (not sudo systemctl start myservice
(without .service
, for example).
Don't care about the "on ALL
computers" part. This is relevant only if you intend to distribute the very same sudoers
file to multiple computers.
You would then change your backup script to
# Stop myservice
sudo systemctl stop myservice.service
# Do all the backing up here...
/path/to/backup_command --with --some --options
# Start myservice
sudo systemctl start myservice.service
and have it run as user noslenkwah
.
answered Dec 21 '18 at 19:18
PerlDuckPerlDuck
5,85111333
5,85111333
add a comment |
add a comment |
Run this shell script as root.
For example, put it in the root crontab:
sudo crontab -e
add a comment |
Run this shell script as root.
For example, put it in the root crontab:
sudo crontab -e
add a comment |
Run this shell script as root.
For example, put it in the root crontab:
sudo crontab -e
Run this shell script as root.
For example, put it in the root crontab:
sudo crontab -e
answered Dec 21 '18 at 16:47
EASYEASY
515
515
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%2f1103632%2fhow-do-i-start-and-stop-a-systemctl-service-inside-a-bash-script%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