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.

    for server in `more server-list.txt`
   sshpass -p password scp -r /root/list/*.* root@$server:/etc/list/
sleep 5
for server in `more server-list.txt`
   sshpass -p password ssh root@$server /sbin/reboot

After I execute the following command to run the script, I only have following output


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


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"
  echo "$server - Failed to reboot"
  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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.