So I want to run this script continuously in background. I was able to start it at boot and then run it, but it stops running after sometime. Whats wrong?
#!/bin/sh
### BEGIN INIT INFO
# Provides: myfirst
# Required-Start: $network
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: myfirst
# Description: Speaker switch
### END INIT INFO
echo "18" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio18/direction
while true;do
ps cax | grep hairtunes > /dev/null
if [ $? -eq 0 ]; then
echo "0" > /sys/class/gpio/gpio18/value
else
echo "1" > /sys/class/gpio/gpio18/value
fi
sleep 5
done
exit 0
You shouldn't run while loop inside initscript. Init will kill long-running initscripts. You can extract all logic
echo "18" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio18/direction
while true;do
ps cax | grep hairtunes > /dev/null
if [ $? -eq 0 ]; then
echo "0" > /sys/class/gpio/gpio18/value
else
echo "1" > /sys/class/gpio/gpio18/value
fi
sleep 5
done
into separate scrip and run it with &
from initscipt. Like this:
#!/bin/sh
### BEGIN INIT INFO
# Provides: myfirst
# Required-Start: $network
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: myfirst
# Description: Speaker switch
### END INIT INFO
case "$1" in
start)
/path/to/the/script/above/with/logic.sh &
;;
stop)
# you need to store pid to do this
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
This should work. However, this isn't a best solution. The best one is to correctly demonize you process which is somewhat different topic.
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.