skip to Main Content

I am new to Linux environment and working on writing scripts to start and stop few services(nodejs app bundled into executables using ‘pkg’ module). I want to stop processes by name and found ‘killall’ command. I tried this command individuaaly and inside bash script. Problem I am facing is, after executing kill command, control does not comeback to terminal and i need to use ctrl+c to get terminal back.

Here is script i tried:

#!/bin/bash
# Run with command : chmod +x /root/myApp/stopserv.sh && /root/myApp/stopserv.sh

echo "Stopping Service1"
nohup killall  Service1 &>/dev/null &
echo "Stopping Service2"
nohup killall  Service2 &>/dev/null &
echo "Stopping Service3"
nohup killall  Service3 &>/dev/null &
echo "Stopping Service4"
nohup killall  Service4 &>/dev/null &

And when i run this script, I get response on terminal like below:

root@Phantom-E03E:~/myApp# chmod +x /root/myApp/stopserv.sh && /root/myApp/stopserv.sh
Stopping Service1
Stopping Service2
Stopping Service3
Stopping Service4
root@Phantom-E03E:~/myApp# /root/myApp/startserv.sh: line 17: 29535 Terminated    nohup ./Service1 &> /dev/null
/root/myApp/startserv.sh: line 11: 29533 Terminated              nohup ./Service2-linux &> /dev/null
/root/myApp/startserv.sh: line 14: 29534 Terminated              nohup ./Service3-linux &> /dev/null
/root/myApp/startserv.sh: line 8: 29527 Terminated              nohup ./Service4-linux &> /dev/null

I want to check:

  1. Is there any other recommended way to stop executables in linux by name?
  2. How to i get control back to terminal “root@Phantom-E03E:~/myApp#” after running script?

Thanks,
Pooja

2

Answers


  1. Just call the script like:

    <path_to_script> && exit
    
    Login or Signup to reply.
  2. You can do it much better. Actually you can do it in two ways
    1. using foreground-background mechanism
    2. using systemd – service management ( a lit bit complicated )

    using fg / bg

    When we run a process using shell (? bash) it will have interactively with our terminal. We can manage this interactivity by

    • Control-Z (stop it, not quit, just pause)
    • Control-C (stop it, kill
      == quit)
    • fg ( bringing it back to foreground == interactivity )
    • bg ( running it in background when it is stopped )

    You can use this way manually or using a script. I will show both.
    here is a screen-shot of doing manually

    enter image description here

    using bash

    here is a simple script

    #!/bin/bash
    
    APP_STATE=$1;
    APP_NAME=$2;
    
    # TASKS=( cont stop );
    
    function stop_app(){
        APP_PID=$(pidof $APP_NAME);
        kill -n 19 $APP_PID;
    
        if [[ $? == '0' ]]; then
            echo "$APP_NAME paused!";
        else
            echo "could not pause $APP_NAME";
        fi
    }
    
    function continue_app(){
        APP_PID=$(pidof $APP_NAME);
        kill -n 18 $APP_PID;
    
        if [[ $? == '0' ]]; then
           echo "$APP_NAME continued ...";
        else
            echo "could not continue $APP_NAME";
        fi
    }
    
    case $APP_STATE in
        cont )
            continue_app ;;
        stop )
            stop_app  ;;
    
    esac
    

    and screen-shot of running this script

    enter image description here

    Note bg and fg are interfaces and you can do it in lower level using kill command by sending stop signal and continue signal which my script does it this way.

    >>> kill -l
     1) SIGHUP   2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP
     6) SIGABRT  7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1
    11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
    16) SIGSTKFLT   17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
    21) SIGTTIN 22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ
    26) SIGVTALRM   27) SIGPROF 28) SIGWINCH    29) SIGIO   30) SIGPWR
    31) SIGSYS  34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
    38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
    43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
    48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
    53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
    58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
    63) SIGRTMAX-1  64) SIGRTMAX    
    

    please notice signals 18 and 19.

    For a simple killing a process by name use pkill command.
    For a complex management please take look at /etc/systemd/system directory
    And if you have a Node.js server up and running and its management use either of pm2 or systemd

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search