Bash script with output
I have a server that needs to send daily some files to remote machines.
I have done this script in order to automate this task.
What I need to insert on this script in order to see if this task as been successfully done?
Both tasks, the first task that sends the files and the other task that forces a reboot on that machine.
#!/bin/sh
for server in `more server-list.txt`
do
sshpass -p password scp -r /root/list/*.* root@$server:/etc/list/
done
sleep 5
for server in `more server-list.txt`
do
sshpass -p password ssh root@$server /sbin/reboot
done
#server-list.txt
10.29.0.32 10.29.0.4 10.29.0.11 10.29.0.12 10.29.0.13 10.29.0.14
After I execute the following command to run the script, I only have following output
root@VPS:~#./sendingfiles.sh
root@VPS:~#
Sometimes I have a error saying that the host had timeout connection, but don't tell me which one has failed and I don't know which one as rebooted successfully
Solution 1:
The variable '$?' stores the result of the previous command.
- 0 = success
- 1-127 = documented error
- (-128)-(-1) = OS killed the program with some signal
So perhaps:
sshpass -p password ssh root@$server /sbin/reboot
if [ $? -eq 0 ]; then
echo "$server - Successfully rebooted"
else
echo "$server - Failed to reboot"
fi