2

I have created a simple script to start a process, but I am not sure how to add a stop step since the process is ran/stopped manually

Currently, to start the process, we use a single command line

omrun dssdaemon <a string to run>

Then, to stop it, the usual ps command

ps -ef | grep -i dssdaemon

kill -9 <PID>

I have gotten this far

#!/bin/sh
# Priti's script to start dss daemon
pid=$(ps -ef | grep dssdaemon | grep -v grep | awk '{print $2}')
DSS_START=omrun dssdaemon <a string to run>

$DSS_START; sleep 15;
echo "DSS Daemons is running with PID[S] below" && $pid

The process has some limitation with using pidof, that is why I have created the awk string instead.

I could really use some help with adding a status and kill step.

Something like this

# status dss daemon
DSS Daemon is running with PID[S] below
123456
432145

# start dss daemon
DSS Daemon is started with PID[S] below
123456
432145

# stop dss daemon
DSS Daemon with PID[S] below were killed
123456
432145

1 Answers1

3

Try to use killall or pkill tools. They do send a signal to the process just as kill does. But instead of pid, they look for the process by its name. And if you have several demons running - all of them would got the signal.

killall -9 dssdaemon
White Owl
  • 4,511
  • 1
  • 4
  • 15
  • 2
    And may be try withouth the `-9` to let the process terminate in an orderly fashion. The default SIGTERM should do the trick. Use SIGKILL (-9) as a last resort only. – markgraf Feb 23 '23 at 07:31
  • Thank you for that, I have added it into the script. But I am not sure how to create a status/stop/start like that of systemctl to make this work in a single script. – Priti Patel Feb 23 '23 at 07:38
  • @PritiPatel You could try checking what arguments the script was invoked with [using `$#` and `shift`](https://unix.stackexchange.com/questions/174566/what-is-the-purpose-of-using-shift-in-shell-scripts) – 404 Name Not Found Feb 23 '23 at 13:38