0

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

1

2 Answers 2

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
-1
  1. cat server-list.txt not more server-list.txt (more is paginated)
  2. If you're scripting this them it's probably easier to use a keyless password rather than going through sshpass -p password
  3. Use $() instead of ` eg for server in $(cat server-list.txt)

Finally Some echo statements in your script

P.s. This question would be better over at SuperUser

1
  • This answer would be perfect for Code Review, but it doesn't answer the actual question here at all. Oct 22, 2020 at 8:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .