简体   繁体   中英

Bash script multiple commands issue

I am currently working on a program that needs to boot a program automatically whenever it registers that this program is not open already. It needs superuser rights to boot. Currently, I have a working Bash script, looking as follows:

#!/bin/bash

while true; do #Continue indefinitely

    if [ $(ps aux | grep '/odroid_detection' | grep -v '<defunct>' -c) -le 4 ]; then #if less than 3 odroid_servers are active (booter opens 3 processes)
        xterm -iconic -e su -c "xterm -iconic -hold /home/odroid/Documents/SUNRISE-Odroid/_odroid_detection/_odroid_detection/bin/Debug/_odroid_detection" 
    fi

    sleep 60 #check every minute

done

The program that executes, however, is not working exactly as planned because it is executed from the root map instead of the map it is in. I therefore want to cd to the map the executable is in (~/Documents/SUNRISE-Odroid/_odroid_detection/_odroid_detection/bin/Debug) but have the same functionality as mentioned above. This is what I came up with:

#!/bin/bash

while true; do #Continue indefinitely

    if [ $(ps aux | grep '/odroid_detection' | grep -v '<defunct>' -c) -le 4 ]; then #if less than 3 odroid_servers are active (booter opens 3 processes) 
        xterm -iconic -e "cd ../_odroid_detection/_odroid_detection/bin/Debug/ && su -c "xterm -iconic -hold -e _odroid_detection""
    fi

    sleep 60 #check every minute

done

This does not work, however, and I have tried many alternatives but I cannot seem to get it working.. It gives the following errors in the terminal:

xterm: Can't execvp cd ../_odroid_detection/_odroid_detection/bin/Debug && su -c xterm: No such file or directory

The xterm that gives this error opens in the map ~/Documents/SUNRISE-Odroid/Bash, and executing the cd mentioned above does work when I execute it seperately, so I do not understand why it cannot find the file or directory.

Any suggestions?

The colouring of StackOverflow made me understand one mistake that I made: the starting quote after 'su -c' gets interpreted as an ending quote of the xterm execute line. The working code is as follows:

#!/bin/bash

while true; do #Continue indefinitely

    if [ $(ps aux | grep '/odroid_detection' | grep -v '<defunct>' -c) -le 2 ]; then #if less than 3 odroid_servers are active (booter opens 3 processes)
        xterm -iconic -e "cd ../_odroid_detection/_odroid_detection/bin/Debug/ && su -c ./_odroid_detection"
    fi

    sleep 60 #check every minute

done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM