Advertisement
METAJIJI

bash_lab1

Apr 26th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.65 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4. get_cmd(){
  5.     ps --no-headers -p $(pidof "$1") -o cmd
  6. }
  7.  
  8. get_pid(){
  9.     if [[ $(pidof "$1") =~ ^[0-9]+$ ]]; then
  10.         return 0
  11.     else
  12.         return 1
  13.     fi
  14. }
  15.  
  16. # Read initial data from user.
  17. until [[ $sec =~ ^[0-9]+$ ]]; do
  18.     read -p "check period (sec): " sec
  19. done
  20.  
  21. until ( get_pid "$name" ); do
  22.     read -p "process name: " name
  23. done
  24.  
  25. # Get cmdline by process name.
  26. cmd=$(get_cmd "$name")
  27. [ -z "$cmd" ] && echo "ERROR: cmdline by programm name $name is not found!"
  28.  
  29. while true ; do
  30.     # ( get_pid "$name" ) || $cmd
  31.     if ( get_pid "$name" ); then
  32.         echo "Waiting, sleep for $sec sec..."
  33.     else
  34.         echo 'Run cmd...'
  35.         $cmd
  36.     fi
  37.     sleep $sec
  38. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement