0

I'v been trying to understand this code but I couldn't get the whole of it. I got that 'init' is the first startup process and that this program is used to start or end a particular mentioned service. But what does the 'cat' in the 'kill' do ?And what happens in the system internal when this program is start, stop and restart?

#!/bin/sh
test -f /usr/sbin/sshd || exit 0
case "$1" in
start)
echo -n "Starting sshd: sshd"
/usr/sbin/sshd
echo "."
;;
stop)
echo -n "Stopping sshd: sshd"
kill `cat /var/run/sshd.pid`
echo "."
;;
restart)
echo -n "Stopping sshd: sshd"
kill `cat /var/run/sshd.pid`
echo "."
echo -n "Starting sshd: sshd"
/usr/sbin/sshd
echo "."
;;
*)
echo "Usage: /etc/init.d/sshd start|stop|restart"
exit 1
;;
esac
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • I think you've confused `init` the process and `init.d` script which start services. – Etan Reisner Oct 16 '15 at 03:04
  • I might have. Can u explain me the code please. – Shreyas Wade Oct 16 '15 at 03:07
  • That's just a normal shell script. You just need to read it to see what it does. If you don't know what `cat` or `kill` are then I suggest you look at the man pages for them. That init script (the service starting scripts that live in init.d are called "init scripts" which is probably what confused you) is the script for the `sshd` service/daemon. `init` the process/program has a man page as well. – Etan Reisner Oct 16 '15 at 03:11
  • Welcome to SO. SO is not the best place to ask questions of the type "please explain the code". It works best when you have a particular error or problem. What you are asking is related to basic *nix commands. I suggest something like (basic unix commands)[http://www.thegeekstuff.com/2010/11/50-linux-commands/] or google `unix command line getting started`. – wwkudu Oct 16 '15 at 03:15
  • But what happens IN the system when the program is run? – Shreyas Wade Oct 16 '15 at 03:16

1 Answers1

0

When a Unix OS is starting up it will run all the scripts in /etc/init.d, among them this one, with the "start" argument. The script then invokes /usr/sbin/sshd which forks a background process and terminates. The background process will again fork and its child will become a demon process. The demon process writes its PID into /var/run/sshd.pid and begins servicing. Upon shutdown of the OS, this script will again be called with the "stop" argument. It will gather the demon's PID from the file and terminate it gracefully (without switch kill sends SIGTERM as if 'kill -15 $pid').

yacc
  • 2,915
  • 4
  • 19
  • 33
  • `it will run all the scripts in /etc/init.d` -- that's not entirely true. There are a number of scripts in `/etc/init.d` that are only run depending on the `runlevel` (now `systemd target`). Which were run under the old init script startup were determined by soft-links in`/etc/init.d/rcS.d/` and then `/etc/init.d/rc[0-6].d/` for any given runlevel `[0-6]`. There were still other scripts in `/etc/init.d/` that were for packages installed but not configured to run at boot. – David C. Rankin Oct 16 '15 at 05:29