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:
- Is there any other recommended way to stop executables in linux by name?
- How to i get control back to terminal “root@Phantom-E03E:~/myApp#” after running script?
Thanks,
Pooja
2
Answers
Just call the script like:
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
== quit)
You can use this way manually or using a script. I will show both.
here is a screen-shot of doing manually
using bash
here is a simple script
and screen-shot of running this script
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.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
directoryAnd if you have a Node.js server up and running and its management use either of pm2 or systemd