Stop apache service remotely

Dear Sirs,

i tried to run this command, but fail at line 3. it fail to read server.txt.

#!/usr/bin/bash
server=$(cat server.txt)
while read -r "$server"
do
echo "$server ..."
ssh userid@"$server" /bin/bash << "_EORS_"
for status
do
echo 3 | sudo tee systemctl status httpd > web.log;
if grep -q "Inactive" > web.log;
then
echo "service is not running"
else
sudo systemctl stop httpd
fi
done
_EORS_
done < server.txt
~

$ ./abc
./abc: line 3: read: `server1': not a valid identifier

thank youus

must be
while read -r server

1 Like

The above is completely unnecessary, if you have

while read -r server
#[...]
done < server.txt

for status - what status, where do you pass the status variables to the script?
(/bin/bash is missing -s $list_of_statuses - unquoted if you intend to use word splitting)
Is a "for loop" even necessary? Do you understand what it's normally used for?

Are you sure, you don't need to read our previous replies again? From this thread...

echo 3 | sudo tee systemctl status httpd > web.log; - this line creates 3 files in your current working directory: systemctl, status and httpd, all owned by root and containing 3; web.log will also contain stdout from sudo tee ... execution (it DOES NOT generate status from systemctl) - so all files will contain just a number 3.
You're better of running sudo systemctl status httpd &> web.log; instead of echo 3 | sudo tee systemctl status httpd > web.log;.

Did you mean if grep -q "Inactive" web.log; (otherwise why are you overwriting web.log with a null output from grep -q - that is, even if you first provided any stdin to it, because it won't work without a filename provided as an argument)

1 Like

Systemd has is-active option which you can utilize, without the need for greps and statues.
Other then broken code stuff written...

systemctl --quiet is-active httpd && systemctl stop httpd

If service is active stop it.

Systemd has many options, and combined with DBUS you can even do stuff over network.
A secure one hopefully :slight_smile:

Regards.
Peasant.

1 Like

Dear Sirs,
thank youss so much.
sirs, i added another server in server list.
it seem did not run in the second server.

kindly find the updated script:


bash-4.3$ cat testc
#!/usr/bin/bash
#server=$(cat server.txt)
while read -r server
do
echo "$server ..."
ssh userid@"$server" /bin/bash << "_EORS_"
STATUS="$(systemctl is-active httpd.service)"
if [ "${STATUS}" = "active" ]; then
sudo systemctl stop httpd
else
echo "service is not running"
fi
_EORS_
done < server.txt

output:

./testc
server1 ...
service is not running
server2 ...
bash-4.3$

sirs, why it did not run the command in the second server..

thank youuus

"Why it did not run the command" is rather difficult to tell, given no information about the actual environment and specifics of your input file.
I would first check (at the least, but not limited to) the following:

Is this the exact complete output you get when running the script?
Aren't there any errors displayed?

Was the second server (server2) added as a separate line of text to server.txt?
Is it followed by a newline character?
What is the file (and newline chars) encoding for server.txt? (post the result of file server.txt command).

Did ssh manage connecting to the second server? (you only have echo $server prior to ssh)
Is the same user account (userid) created/used in server2?
Does the second server run systemd as its init system (hence does it use systemctl as well)?
Is the httpd.service installed on it?

1 Like

I think it ran the stop command but it was silent.

You can simplify this using the exit status:

if systemctl is-active httpd.service; then

Suppress the output with

if systemctl -q is-active httpd.service; then
2 Likes

Dear Sir,
apologies for late.
udpate, i tried to run the script, seem the script stop the service in both server.
$ cat server.txt
server1
server2

the new output is as per below:

$ ./finasd
server1 ...
active
service is not running
server2 ...
active
service is not running

thank youuss so much sir

Dear Sir,
thank youss
i checked the forum and have updated the script is as per below:

#!/usr/bin/bash
#server=$(cat server.txt)
while read -r server
do
echo "$server ..."
ssh fastuser@"$server" /bin/bash << "EORS"
systemctl is-active httpd.service && sudo systemctl stop httpd
echo "service is not running"
EORS
done < server.txt
$

the output:
$ ./testd
server1 ...
active
service is not running
server2 ...
active
service is not running
thank youss so much sir

Dear Sirs,

i have added date output in the script:

#!/usr/bin/bash
#server=$(cat server.txt)
while read -r server
do
echo "$server ..."
current_date=$(date)
echo "Current date: $current_date"
ssh userid@"$server" /bin/bash << "_EORS_"
systemctl is-active httpd.service && sudo systemctl stop httpd
echo "service is not running"
_EORS_
done < server.txt

output

$ ./teste
server1 ...
Current date: Mon Jun 10 09:02:58 +08 2024
inactive
service is not running
server2 ...
Current date: Mon Jun 10 09:02:58 +08 2024
inactive
service is not running

truly appreciate sirs.
thank yousss so much.

Dear Sirs,
would like to seek g8 advise.

i tried to output the log file using tee, but the command only output small portion.

#!/usr/bin/bash
#server=$(cat server.txt)
while read -r server
do
echo -e "\e[32m $server ...\e[0m" | tee /tmp/test/test.log
current_date=$(date)
echo -e "\e[34m Current date: $current_date \e[0m" | tee -a /tmp/test/test.log
ssh userid@"$server" /bin/bash << "_EORS_"
systemctl is-active httpd.service && sudo systemctl stop httpd
echo -e "\e[31m service is not running \e[0m" | tee -a /tmp/test/test.log
_EORS_
done < server.txt

output:

bash-4.3$ ./testf
 server1 ...
 Current date: Tue Jun 11 14:42:46 +08 2024
inactive
tee: /tmp/test/test.log service is not running
: No such file or directory
 server2 ...
 Current date: Tue Jun 11 14:42:47 +08 2024
inactive
 service is not running
bash-4.3$ cat test.log
 server2 ...
 Current date: Tue Jun 11 14:42:47 +08 2024
bash-4.3$

thank youu

The code between the _EORS_ runs on the remote host. Writing to /tmp/test/test.log requires that /tmp/test/ is present on it (i.e. on each remote host).
Perhaps you want to write on the local (calling) host. Then either redirect each ssh call

ssh userid@"$server" /bin/bash << "_EORS_" | tee -a /tmp/test/test.log

or redirect the whole loop:

while read -r server
do
...
done < server.txt | tee -a /tmp/test/test.log

The latter will include the echoed
$server ...

1 Like

thankkss youuss so much sir.. truly appreciate it..

thankkss youuss so much sir.. truly appreciate it..