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
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)
"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?
#!/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
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$
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