4

I have two commands below and I don't know what they are doing.

What does these commands do respectively?

kill -USR1 %1
kill $(pidof vlc)

2 Answers 2

4

kill -USR1 %1 sends the "user-defined signal #1" (a.k.a. "SIGUSR1") to the first background child process of the current shell process. If that background process has set up a signal-handler function for the USR1 signal, that function will be run. If the target process doesn't have a signal-handler for that signal, the target process will terminate.

kill $(pidof vlc) sends the "terminate" (SIGTERM) signal to the vlc process.

Let's walk through the full scenario OP described in a comment on another Answer:

A user opens a new window with a Unix shell on her desktop and starts two programs as follows:
emacs &

This launches the Emacs text editor in the background, making it the first background job (%1).

vlc ti2_intro.mp4

This launches the VLC video player in the foreground to play the ti2_intro.mp4 video.

While the Videoti2_intro.mp4 is being played, the user presses Ctrl-z in the shell window.

This interrupts the foreground child process, which is VLC. That means VLC has been temporarily halted but not terminated. It's in a suspended state. It is now process %2.

Now [she issues] the shell command jobs,

That lets her see what child processes are attached to this shell process. It probably lists emacs as %1, and vlc as %2

followed by:
bg %2

This resumes VLC in the background, so that the video can still play, without tying up the shell, so she can issue more shell commands while it's playing.

kill -USR1 %1

This sends the "user defined signal 1" to emacs. Emacs lets users define their own signal handlers, so it's hard to know what this signal did without knowing details of her Emacs setup. It's often used to trigger the debugger in the Emacs "elisp" Lisp programming environment.

A little later she gives the shell command [I think OP left out a command here?] and then presses Ctrl-c.

It's unclear what command OP may have left out here, but I'm guessing she ran a command that didn't exit immediately, and then used Ctrl-c to exit it.

Shortly thereafter she gives the following commands in the same Shell window:
kill $(pidof vlc)

This kills (terminates) the VLC process.

-1

kill terminates a listed process (or sends a signal to the process asking it to kill itself).

In the first case a signal is sent being sent to the progress to ask it to kill itself (which it will only do if it has been coded to - a program so coded will clean up after itself and exit). %1 means "job1" Job1 is a process that is running in the background and associated with your current session (See What is the meaning of kill %1)

In the second case the command is finding the process IDs of vlc and terminating them.

3
  • All in all it looks like this: Scenario: A user opens a new window with a Unix shell on her desktop and starts two programs as follows: -> emacs & -> vlc ti2_intro.mp4 While the Videoti2_intro.mp4 is being played, the user presses Ctrl-z in the shell window. Now it gives the shell command job, followed by: -> bg% 2 -> kill -USR1% 1 A little later she gives the shell command and then presses Ctrl-c. Shortly thereafter she gives the following commands in the same Shell window: -> kill $ (pidof vlc)
    – Abraham
    Dec 6, 2020 at 21:46
  • kill just sends a signal, it doesn't have to be a signal for the process to kill itself. For example, kill -HUP just sends a SIGHUP to the process, it doesn't cause the process to kill itself (unless of course that is part of the signal handler for that specific program defined with the SIGHUP signal).
    – MaQleod
    Dec 6, 2020 at 22:00
  • To the down voter - the man page for kill summary states "kill - terminate a process". Further my post clarifies the behaviour for sigusr.
    – davidgo
    Dec 7, 2020 at 1:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .