Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import signal
  2. import time
  3.  
  4. def signal_handler(sig, frame):
  5. print('sigint')
  6. quit(0)
  7.  
  8. signal.signal(signal.SIGINT, signal_handler)
  9. print ('aaa')
  10. while (1):
  11. time.sleep(1.5)
  12.  
  13. #!/bin/bash
  14.  
  15. #set -x
  16.  
  17. DIR="/home/pi"
  18. PRESCRIPT="source venv/bin/activate;python --version"
  19. SCRIPT="python prg.py"
  20.  
  21. EXITDO=0
  22. PID=-1
  23. trap ctrl_c INT
  24. function ctrl_c() {
  25. echo 'SIGINT intercepted.'
  26. EXITDO=1
  27. }
  28.  
  29. #Main loop
  30. while [ $EXITDO -eq 0 ]; do
  31. echo 'Starting process…'
  32. cd "$DIR"
  33. eval $PRESCRIPT
  34. $SCRIPT &
  35. PID=$(echo $!)
  36. echo "Process started with PID: $PID"
  37. while $(kill -0 $PID) && [ $EXITDO -eq 0 ]; do
  38. sleep 1
  39. done
  40. if [ $EXITDO -eq 0 ]; then
  41. echo 'Process terminated. Restart.'
  42. else
  43. echo 'Kill service.'
  44. kill -2 $PID
  45. fi
  46. done
  47.  
  48. #!/bin/sh
  49. ### BEGIN INIT INFO
  50. # Provides: prg
  51. # Required-Start: $local_fs $network $named $time $syslog
  52. # Required-Stop: $local_fs $network $named $time $syslog
  53. # Default-Start: 2 3 4 5
  54. # Default-Stop: 0 1 6
  55. # Description: <DESCRIPTION>
  56. ### END INIT INFO
  57. NAME=prg
  58. SCRIPT="/home/pi/prg.sh"
  59.  
  60. PIDFILE=/var/run/$NAME.pid
  61.  
  62. start() {
  63. if [ -f $PIDFILE ] && [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE); then
  64. echo 'Service already running' >&2
  65. return 1
  66. fi
  67. echo 'Starting service…' >&2
  68. #local CMD="$SCRIPT &> "$LOGFILE" & echo $!"
  69. #su -c "$CMD" $RUNAS > "$PIDFILE"
  70. # Try with this command line instead of above if not workable
  71. # su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"
  72. $SCRIPT &
  73. echo $! > "$PIDFILE"
  74. PID=$(cat $PIDFILE)
  75. if kill -0 $(cat $PIDFILE) > /dev/null
  76. then
  77. echo "$NAME is now running, the PID is $PID"
  78. else
  79. echo ''
  80. echo "Error! Could not start $NAME!"
  81. fi
  82. }
  83.  
  84. stop() {
  85. if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
  86. echo 'Service not running' >&2
  87. return 1
  88. fi
  89. echo 'Stopping service…' >&2
  90. #Kill by pid
  91. kill -2 $(cat "$PIDFILE")
  92. #remove .pid file
  93. sleep 3
  94. if ! kill -0 $(cat "$PIDFILE"); then
  95. rm -f "$PIDFILE"
  96. echo 'Service stopped' >&2
  97. fi
  98. }
  99.  
  100. uninstall() {
  101. echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
  102. local SURE
  103. read SURE
  104. if [ "$SURE" = "yes" ]; then
  105. stop
  106. rm -f "$PIDFILE"
  107. echo "Notice: log file was not removed: $LOGFILE" >&2
  108. update-rc.d -f $NAME remove
  109. rm -fv "$0"
  110. else
  111. echo "Abort!"
  112. fi
  113. }
  114.  
  115. status() {
  116. printf "%-50s" "Checking $NAME..."
  117. if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then
  118. PID=$(cat $PIDFILE)
  119. if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
  120. printf "%sn" "The process appears to be dead but pidfile still exists"
  121. else
  122. echo "Running, the PID is $PID"
  123. fi
  124. else
  125. printf "%sn" "Service not running"
  126. fi
  127. }
  128.  
  129.  
  130. case "$1" in
  131. start)
  132. start
  133. ;;
  134. stop)
  135. stop
  136. ;;
  137. status)
  138. status
  139. ;;
  140. uninstall)
  141. uninstall
  142. ;;
  143. restart)
  144. stop
  145. start
  146. ;;
  147. *)
  148. echo "Usage: $0 {start|stop|status|restart|uninstall}"
  149. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement