I need to execute a command or a script and wait for the end of executing (ash)

i need to execute a command or a script and after that wait for the end of executing of the command
not to do anything else before app.sh done
script /root/app.sh

echo " Somthing else12 34 "
sleep 10
echo " Somthing 12 56 34 "
sleep 10
echo " Somthing end "

script /root/run.sh

/root/app.sh
wait ? how to wait till the end app.sh

for now i use sleep 21 but maybe there is some other way

/root/app.sh && do something

1 Like

I do not understand the issue here... the default behavior is to wait.

3 Likes
ping -c 1 192.168.1.5
    if [ $? = "0" ]; then
    ssh root@192.168.1.5 /root/wifi_up
    if [ $? = "0" ]; then
    logger "exec: ssh root@192.168.1.5 /root/wifi_up ok"
    else
    logger "exec: ssh root@192.168.1.5 /root/wifi_up ko"
    fi
fi
1 Like

When I connect remotely like ssh - y root@192.168.222.1 '/root/run.sh'
The run.sh script will be executed and ssh connection will be lost so the app.sh which started by moment when connection wasn't lost yet will be stopped, so for now I just put sleep 22
to be sure that script app.sh will be donne within 22 seconds
So is there any command wich can wait till last command is done

verify that 192.168.222.1 can be reached

execute the remote command

if there are errors it notifies you (via echo and/or logread)

put this script on the source machine

ping -c 1 192.168.222.1
if [ $? = "0" ]; then
ssh -y root@192.168.222.1 /root/run.sh
if [ $? = "0" ]; then
logger "command exec"
echo "command exec"
else
logger "command exit fail"
echo "command exit fail"
fi
fi
1 Like

quote:
ssh connection will be lost

the ssh connection is not lost, simply the command is executed, it's up to you whether you want to know if the command gave errors or not

the command /root/run.sh what does he do ?

I insist this works as expected...

root@router:~# cat app.sh 
echo " Somthing else12 34 "
sleep 10
echo " Somthing 12 56 34 "
sleep 10
echo " Somthing end "
root@router:~# cat run.sh 
/root/app.sh

echo "Run ends here"
root@router:~# ./run.sh 
 Somthing else12 34 
 Somthing 12 56 34 
 Somthing end 
Run ends here
[eperez@delfin test]$ ssh root@router /root/run.sh
 Somthing else12 34 
 Somthing 12 56 34 
 Somthing end 
Run ends here

In your real use case, are you calling a script, or an app that goes into background?

3 Likes

yes it is init.d scropt that runs mykikaconsole.sh in background

#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org

START=99
STOP=1

start() {        
       /bin/ash /bin/mykika/mykikaconsole.sh 10 &
sleep 12
}                 
 
stop () {
	kill -9 $(ps | grep 'mykika' | grep ash | awk '{print $1;}')

}

shutdown() {
	kill -9 $(ps | grep 'mykika' | grep ash | awk '{print $1;}')

}

but notice mykikacinsole script has deley before start 10 so i had to put sleep 12 to be sure that init.d script which executes mykikaconsole will wait and remotly wont close executing

The & sign at the ent of the command in the start function makes that line "non-blocking". It allows the function to continue instead of waiting for that line to finish by putting that line in a background process.

Maybe just remove the & and let that script block your function instead of make it explicitely non-blocking and try to guess how long it took.

2 Likes

i cant remove & it starts endless loop as a process
i just thought maybe there some kind of sleep wich can see if last command done
but as i understand now it is impassible to know cause it is in background

thanks to everyone now i see there is no way to do what i want .and i should have explain my question better
from the beginning of topic
maybe someone know a way to delay starting of my init script but only when router just started
(my script wont work till hostapd fully started )

this verifies that radio1 or radio0 is up and runs the script
you can put it in /etc/rc.local before exit 0, with sleep appropriate to when you think your wireless network will be up
or put it in a crontab to run every minute

WIFI=$(wifi status radio0 | grep "\"up\": true" | wc -l)
if [ ${WIFI} = "1" ]; then
/bin/mykika/mykikaconsole.sh
fi

WIFI2=$(wifi status radio1 | grep "\"up\": true" | wc -l)
if [ ${WIFI2} = "1" ]; then
/bin/mykika/mykikaconsole.sh
fi
1 Like

good idea but i use init.d script for running my script
can i put this in my /etc/inid.d/mykika in start function?
but if i do it will check it only once so i have to make endless loop for cheking untill all wifis is up

then this script needs to be run 1 time

/etc/rc.local (only boot)


Or does it have to be done more than once?

crontab every minutes

#min hour day month day-week command
#0-59 0-23 1-31 1-12 0-6(0=Sunday) exec
* * * * * /root/your_script

it has to be run on start of router and keep working till the end (as a demon or a service )
the script has settings up section and then endless loop which starts some other scripts in background as well

this is from top

29836     1 root     S     1320   2%   0% /bin/ash /bin/mykika/mykikaconsole.sh 10

and this is from ps but that is the same

 8516 root      1308 S    sleep 4
 8517 root      1316 R    ps
22310 root      1228 S    /usr/sbin/dropbear -F -P /var/run/dropbear.1.pid -p 22 -K 300 -T 3 -2 9
22312 root      1312 S    -ash
23928 root      1228 S    /usr/sbin/dropbear -F -P /var/run/dropbear.1.pid -p 22 -K 300 -T 3 -2 9
23929 root      1308 S    -ash
24130 root      1228 S    /usr/sbin/dropbear -F -P /var/run/dropbear.1.pid -p 22 -K 300 -T 3 -2 9
24131 root      1308 S    -ash
29836 root      1320 S    /bin/ash /bin/mykika/mykikaconsole.sh 10

########################### MY KIKA V0.6.2b #################################################################################
#!/bin/ash
delaystart=$1
#if [ "$delaystart" = "" ]
if [ -z "${delaystart}" ]
			then
delaystart=0
fi
sleep $delaystart
######################## READING CONFIG  ##########################################################
for config in log_lvl work_dir tmp_dir threshold periud guest_wlan dummy_ban_time dummy_delay_decision delay_offer delay_chek_roam
do
eval $config=$(echo -e "$(cat /etc/config/mykikaconfig)" |grep $config | awk -F ' ' '{ print $2 }')
done
############################################################################################
mylog="${tmp_dir}kikalog.log"
allcandidates="${tmp_dir}kikacandidates"
echo -n > $allcandidates
dummyst=$(cat ${work_dir}lists/dummies)
neigbors=$(cat ${work_dir}lists/neighbors)
neigborsguest=$(cat ${work_dir}lists/neigborsguest)

echo -e "$(date +"%d.%m.%y %T") AVNG WiFi 802.11rkv system started" >> $mylog



if [ $log_lvl -le 4 ]; then logger -p warn -t MYKICKA "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||";fi
if [ $log_lvl -le 4 ]; then logger -p warn -t MYKICKA "|||         log_lvl:$log_lvl      ||   work_dir:$work_dir ||        tmp_dir: $tmp_dir        ||    threshold:$threshold dB   ||       periud:$periud  sec.    |||";fi
if [ $log_lvl -le 4 ]; then logger -p warn -t MYKICKA "|||   guest_wlan:$guest_wlan     ||  dummy_ban_time:$dummy_ban_time ms  ||  dummy_delay_decision:$dummy_delay_decision sec. ||  delay_offer:$delay_offer sec.   ||  delay_chek_roam:$delay_chek_roam sec. |||";fi
if [ $log_lvl -le 4 ]; then logger -p warn -t MYKICKA "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||";fi


wlanlist=$(ifconfig | grep wlan | grep -v sta | awk '{ print $1 }')
if [ $log_lvl -le 1 ]; then echo "|||$(date +"%T")  Starting up! .. "; fi
if [ $log_lvl -le 1 ]; then echo "|||$(date +"%T")  Setting neighbors .. "; fi
if [ $log_lvl -le 3 ]; then logger -p warn -t MYKICKA " Setting parametrs $mylog $allcandidates $threshold $periud wlanlist: $wlanlist "  !!;fi

################################################ GETTING NUMBER OF NEIGHBORS #############################
for wlan in $wlanlist
	do
		neigi=$neigbors
		if [ "$wlan" = "$guest_wlan" ]
			then
			neigi=$neigborsguest
		fi
		y=0;
		for i in $neigi; 
			do 
 				y=$((y+1)); 
			done
##########################################################################################################
if [ $y -ne 0 ]
	then
############################################### SETTING NEIGHBORS AND FULL OFFER LIST #####################
OUTPUT="[["
COUNT=0
for ELEMENT in $neigi;
 do
        OUTPUT="$OUTPUT$ELEMENT]"
        COUNT=$((COUNT+1))
        [ $COUNT -eq $y ] && break
        OUTPUT="$OUTPUT,["
done
OUTPUT="$OUTPUT]"
######################
ubus call hostapd.$wlan bss_mgmt_enable "{ 'neighbor_report': true, 'beacon_report': true, 'link_measurements': true, 'bss_transition': true }"
ubus call hostapd.$wlan rrm_nr_set '{ "list":[] }'
ubus call hostapd.$wlan rrm_nr_set '{ "list": '$OUTPUT'}'
if [ $log_lvl -le 1 ]; then echo "|||$wlan neigi:$OUTPUT"; fi
if [ $log_lvl -le 1 ]; then logger -p notice -t MYKICKA "neigbors for:| $wlan: $OUTPUT| ";fi

echo -e "$(date +"%d.%m.%y %T") $wlan neigbors: $OUTPUT " >> $mylog
if [ $log_lvl -le 3 ]; then logger -p notice -t MYKICKA " Setting parametrs: $wlan neigbors: $OUTPUT ";fi
######################################################################################################
offer="["
count=0
	for neighid in $neigi;
		do
		pipa=$(echo "$neighid" |awk -F ',' '{ print $3 }')
  		offer="$offer$pipa"
        	count=$((count+1))
        	[ $count -eq $y ] && break
        	offer="$offer,"
	done
offer="$offer]"
###########
#echo  " $wlan:$offer " 

echo -e "$wlan:$offer" >> $allcandidates
##########################################################################################################################

echo -e "$(date +"%d.%m.%y %T") $wlan candidates:$offer" >> $mylog
if [ $log_lvl -le 3 ]; then logger -p notice -t MYKICKA "$wlan candidates:$offer ";fi
#########################################################################
if [ $log_lvl -le 1 ]; then echo "|||$wlan candidates:$offer"; fi
#-----------------------------------------------------------------------------------------
###########################################################################################################
fi
	done
##########################################################################################################################################
if [ $log_lvl -le 3 ]; then logger -p notice -t MYKICKA "End of setup ";fi
if [ $log_lvl -le 4 ]; then logger -p warn -t MYKICKA "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Gatering own rmm !!! ||||||||||||||||||||||||||||||||||||||||||||||||||||||||";fi
##########################################

 /bin/ash /bin/mykika/neigi_reporter.sh $log_lvl &


################################################# END OF SETUP ###########################################################################

##########################################################################################################################################

echo  " |$HOSTNAME mykika started |OK| " 
####################################################### MAIN LOOP #####################################################################################################################################################
while :
do
sleep $periud
######################################################loop for each wlan###########################################################################################################################################
for wlan in $wlanlist
do
if [ $log_lvl -le 2 ]; then logger -p notice -t MYKICKA "MAIN LOOP:||$wlan ||  ";fi

##########################################################################################################
###############################################################################################
	maclist=$(iw $wlan station dump | grep Station | awk '{ print $2 }')
	for mac in $maclist
		do
		rssi=$(iw $wlan station get $mac | grep "signal avg" | awk '{ print $3 }')
		if [ $log_lvl -lt 1 ]; then  echo " ||| $(date +"%T") || $mac || $rssi dB || <-- curently in procces ! ||| "; fi

################################################################################################################
##-----------------------LOW RSSI TRIGGER -------------------------------###  #### ### ### ### ###  # # ####
###############################################################################################################

			if [ $rssi -lt $threshold ]
				then
				flag=1
###############################  DUMMY PHONES ####################################################################################################################
				for dymmymac in $dummyst
					do
						if [ "$mac" = "$dymmymac" ]
							then
							flag=0	
#############################################################################################################################								
if [ $log_lvl -le 2 ]; then logger -p notice -t MYKICKA "Sending parametrs to Dummy kicker : $mac  $wlan  ||  ";fi
/bin/ash ${work_dir}execs/kickerdummys.sh $log_lvl $wlan $mac $delay_chek_roam $threshold $dummy_delay_decision $dummy_ban_time   &

if [ $log_lvl -le 2 ]; then logger -p notice -t MYKICKA "Sending parametrs to Dummy kicker (done) :  $mac $wlan  ";fi
##############################################################################################################################
						fi
					done
if [ $log_lvl -lt 1 ]; then echo "||| ------------------------------------ Y =  $y ------- flag $flag ------------------------------------- |||| ";fi

################################  SMART PHONES ############################################################################################################################################################################################
if [ $flag -eq 1 ]
	then
		if [ $y -ne 0 ]
			then
				offer=$(echo -e "$(cat /tmp/kikacandidates)" |grep $wlan | awk -F ':' '{ print $2 }')
				if [ $log_lvl -lt 1 ]; then echo "|||Candidates for $mac : $offer|||"; fi
#################################################################################################################################################################
if [ $log_lvl -le 2 ]; then logger -p notice -t MYKICKA "Sending parametrs to kicker : $offer $mac $y $wlan  ||  ";fi
/bin/ash ${work_dir}execs/kicker.sh $log_lvl $wlan $mac $delay_chek_roam $offer $y $delay_offer $rssi &
if [ $log_lvl -le 2 ]; then logger -p notice -t MYKICKA "Sending parametrs to kicker (done) : $offer $mac $y $wlan  ";fi
##########################################################################################################################
			else			  
				ubus call hostapd.$wlan del_client "{'addr':'$mac', 'reason':5, 'deauth':1, 'ban_time':$dummy_ban_time}"
				if [ $log_lvl -le 1 ]; then echo "||| $(date +"%T") || MAC $mac has been kicked cause  no neigbors has been set !  ||| ";fi

		fi	
fi
#############################################################################################################################################################################################################################################

			fi
		done
done	
done
################################################ END OF LOOP ########################################################################################

too much echo , but i need all this

and this is mykika in init.d

#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org

START=99
STOP=1

start() {        
       /bin/ash /bin/mykika/mykikaconsole.sh 10 &
sleep 12
}                 
 
stop () {
	kill -9 $(ps | grep 'mykika' | grep ash | awk '{print $1;}')

}

shutdown() {
	kill -9 $(ps | grep 'mykika' | grep ash | awk '{print $1;}')

}

and i manage all this from one router using commands like
ssh -y root@$host 'etc/init.d/mykika restart'

look i have this script like runing from rc.d via simlinks (all as usual)
i can do all those /etc/init.d/mykika start stop restart etc
all works
and i can connect from other device via ssh and then do the same /etc/init.d/mykika start stop restart etc

ad all works good BUT
when i use command from that other device like
ssh -y root@192.168.222.101 '/etc/init.d/mykika restart'

it wont wait unless i put sleep (more then need to start mykikaconsole.sh seconds) in /etc/init.d/mykika script
so the ssh -y root@192.168.222.101 '/etc/init.d/mykika restart' will wait untill mykikaconsole.sh fully started
is no sleep after start
ssh -y root@192.168.222.101 '/etc/init.d/mykika restart' will be diconected immediatly after running /etc/init.d/mykika with no sleep in it and the mykikaconsole.sh wont fully started

install "at" package

/etc/rc.local

# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.

if [ -x /bin/mykika/mykikaconsole.sh ]; then
at -f /bin/mykika/mykikaconsole.sh now+2minute
fi

exit 0