You are not logged in.
I am writing a script to toggle a systemctl service:
#! /bin/bash
if [ "$(sudo systemctl is-active cronie.cervice)"="inactive" ];
then
sudo systemctl start cronie.service
echo "cronie started"
else
sudo systemctl stop cronie.service
echo "cronie stopped"
fi
if i run the script it only starts the service but will not stop it no matter what is-active says
I have tried a one-line version with the same problem.
thanks in advance.
Last edited by jl2 (2022-10-23 12:35:46)
Why I run Arch? To "BTW I run Arch" the guy one grade younger.
And to let my siblings and cousins laugh at Arsch Linux...
Offline
add spaces around the = in the if [ ... ]
Offline
It would be better to do
if ! systemctl --quiet is-active cronie.cervice
Last edited by lambdarch (2022-10-23 12:20:51)
aka Tamaranch: https://gitlab.xfce.org/Tamaranch
Offline
To elaborate on astralc's response a bit: You've invoked test/[…] with a single argument, which according to the manpage means it simply tests if that argument is null/empty.
That is never the case, because the string would be either "active=inactive", "failed=inactive", "inactive=inactive", or any of the other cases (all of which would make the string non-null).
If you split it up as suggested by astralc, you now invoke test/[…] with 3 arguments, and (due argument 2 being `=`) instruct it to test for equality between argument 1 and argument 3, which is probably what you want.
If you do not care to handle `failed` as a state, the following would be even more direct (no need for using test/[…] or launching a subshell):
if systemctl is-active --quiet cronie.service; then
# stop service here
else
# start service here
fi
--edit Ah, too slow, the second part is now a slight repetition of lambdarch's suggestion.
Last edited by ayekat (2022-10-23 12:28:52)
Offline
thanks, it works now, i took lambdarchs version
Why I run Arch? To "BTW I run Arch" the guy one grade younger.
And to let my siblings and cousins laugh at Arsch Linux...
Offline