1

To celebrate my 3 month probation as a web developer concluding, I'm going to make a terminal cake for the office. In my research on the subject I came across this photo, and was wondering what the commands do?

cake picture

okeegan
  • 93
  • 2
  • 9
  • 1
    They're supposed to stop any `httpd` (web server) process on the machine. But the command is broken. :-\ – Adam Liss May 02 '14 at 00:20
  • 2
    Well, the values passed to `kill -9` will include more than just process ids, but unless some of them happen to be valid PIDs, it will just print a bunch of error messages and kill the processes anyway. (But on my system, where I run Apache, the web server processes are named `apache2`, not `httpd`.) – Keith Thompson May 02 '14 at 00:26
  • Thanks both, my curiosity has been sated! I guessed it might have been something along those lines. – okeegan May 02 '14 at 00:36
  • Curious. The hostname is "sigint", but the signal is SIGKILL. – kojiro May 02 '14 at 02:04
  • simple trick to eliminate grep -v grep ... use square brackets on one of the letters such as: `grep [h]ttpd` ... wouldn't want to miss a server running in ./bigreport/www – technosaurus May 02 '14 at 02:57
  • 1
    Or do it properly and use `pgrep`/`pkill`. – Adrian Frühwirth May 02 '14 at 06:44

2 Answers2

6

That pipeline tries to stop all of the Apache processes on the system, in a rather heavy-handed Rube Goldbergian fashion.

The pipeline:

  1. Gets a list of all processes on the system. (ps axww)

    This method is an overreach, providing more data than is actually necessary to achieve the desired end, which will cause problems later.

  2. Looks for lines containing httpd, which is a common process name for the Apache web server. It could match other things accidentally, but on a machine that's supposed to just be a web server, it's fairly safe.

    You get lines like this out of this stage:

    17652 ?        Ss     0:00 /usr/bin/httpd -blah -args
    

    The pipeline would match a vi /etc/httpd/conf.d/mime.conf command, too.

  3. Filters out lines containing grep (grep -v grep) because the first grep will also find itself:

    24180 pts/0    R+     0:00 grep httpd
    

    If you don't filter this line out, you risk killing off the first grep instance before the pipeline finishes, thereby breaking the pipeline. ps on Linux sorts its output by PID by default, so since PIDs wrap around, grep could appear before httpd, causing the cake command to actually have no effect at all.

  4. Uses xargs to run kill -9 on every line found.

    That is, it builds commands like this and runs them:

    kill -9 17652 ?        Ss     0:00 /usr/bin/httpd -blah -args
    

    This may or may not do what you want. It can sometimes work because the process ID (PID) is the first thing on the line when you run ps with the axww flags. (There are other ways to run ps where the first thing on the line is something else.) The cake decorator is hoping that the implementation of kill on the system doesn't barf when it gets all the other junk following the PID on the ps output line.

    POSIX doesn't say what kill(1) does with non-PID arguments. It could stop at the first non-numeric argument, it could give errors for each such argument it finds, or it could silently ignore them. If the line found by ps happens to contain numbers that are valid PIDs, the command on the cake could end up killing processes you didn't intend it to.

It would be a lot better to use pgrep here, if it's available:

# pgrep httpd | xargs kill -9

Not only is the command shorter, it does what you actually want, reliably. It doesn't match and then filter out grep processes, it only matches on the process name, and it doesn't pass non-PID junk to kill.

Systems with pgrep often also have the command pkill, which wraps up that pipeline into a single command:

# pkill httpd

You can add -9 here to forcibly terminate httpd processes if you want, but I will leave it off from here on. I've ordered these commands to be increasingly discriminate, so it also makes sense to make them decreasingly brutal, if you will.

If your system doesn't have pgrep or pkill, it may have pidof:

# kill $(pidof httpd)

Yet another method is to use killall:

# killall httpd

Beware, the killall command may do something different on non-Linux OSes.

The safest method is to use your OS's normal "stop the web server" command, however. Examples:

# service httpd stop
# /etc/init.d/httpd stop
# systemctl stop httpd.service
# launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist

Apache includes a "stop Apache nicely" command:

# apachectl stop

That command will only stop Apache proper, however. The OS-specific commands above may do other cleanup actions as well. If Apache was started by the OS, you should use the OS's own command to stop it, too.

Community
  • 1
  • 1
Warren Young
  • 40,875
  • 8
  • 85
  • 101
-2

This is a cake-protection command. This command unables a Root to eat the cake, because after this angry users will call the Root, open trouble tickets, etc. :)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Naeel Maqsudov
  • 1,352
  • 14
  • 23