0

I am trying to call a script from another script because I am using "spawn" (from expect packet) to execute an SSH to another machine. Basically, after executing a comparison and routine in my main script (scriptA.sh) it calls for another script (scriptB.sh). The calls works fine, as I can see that the SSH is correctly executed. However, something looks wrong as not all the commands are executed correctly. If I execute those commands manually it works.

I have running an application on a machine that sometimes gets frozen and shows totally white screen. scriptB.sh kills firefox and re-open the broswer on background. The commands works fine if i execute them manually:

  1. sudo killall firefox
  2. export DISPLAY=:0.0 (I don´t need "sudo" for this)
  3. /usr/local/bin/run_digital_signage_firefox.sh& (This command must be running without "sudo")

This is a very small resume from my scriptA.sh (I execute this script like this: sudo ./scriptA.sh):

#!/bin/bash

...something....

 export username
 export password
 export IP
 sh /home/mydirectory/scriptB.sh

...something....

and this is my scriptB.sh (I pass the username, password and IP using "export" in my scriptA.sh):

#!/bin/bash
   /usr/bin/expect << EOF
   spawn ssh test@$IP "sudo killall firefox && export DISPLAY=:0.0 && /usr/local/bin/run_digital_signage_firefox.sh&"
   sleep 3
   expect "*?ame:*" {
   send "$username\r"
   sleep 2
   expect "*?assword:*"
   send "$password\r"
   sleep 2
   expect "\r"
   sleep 2
   }
   expect "*?(yes/no)*" {
   send "yes\r"
   sleep 2
   expect "*?ame:*"
   send "$username\r"
   sleep 2
   expect "*?assword:*"
   send "$password\r"
   expect "\r"
   sleep 2
   }
EOF

When scriptA.sh calls scriptB.sh it seems that it gets to kill firefox browser but not to initialize this with the command "/usr/local/bin/run_digital_signage_firefox.sh&". I must run this command without sudo but even if i try to add "sudo /usr/local/bin/run_digital_signage_firefox.sh&" in the SSH in scriptB.sh it does not work as well. It seems that the script is not executing "/usr/local/bin/run_digital_signage_firefox.sh&" neither "sudo /usr/local/bin/run_digital_signage_firefox.sh&". I need to execute the last part of the script to open again the firefox browser.

Both scripts have rwx permissions in ugo.

X T
  • 445
  • 6
  • 22
  • 1
    if `sudo killall` returns non-zero, then the rest of the chained commands will not execute. Try changing `&&` to `;` – glenn jackman Feb 18 '21 at 22:12
  • Thanks for your answer glenn. Non-zero value is returned when sudo killall is successsful? – X T Feb 19 '21 at 07:19
  • 1
    non-zero exit status generally means an error (depends on the tool, but that's the convention). Perhaps if killall finds no processes to kill, it might return non-zero. Check the man page. – glenn jackman Feb 19 '21 at 16:15
  • Ok I will check then. Usually, "killall firefox" finds process to kill as I have my browser firefox open. If ";" does not work there is any another tip I can use to resolve this issue? – X T Feb 19 '21 at 20:14
  • Ok it is resolved. I added ";" and also the PATH of the machine where I SSH. Without the PATH it seems that don't recognize those commands in destination machine. Just do "echo PATH" and add this on Spawn SSH... With the rest of the command. – X T Feb 22 '21 at 21:09

0 Answers0