If two jobs are entered into bash command line separated by semicolon:

$ job 1; job 2;

How do I cancel the second one? If I press CTRL+c, the first one will be canceled and 'job 2' would start, I just want to stop second job not to be executed. I tried atq, ps but no luck, with ps -ef I can see only 'job 1' but 'job 2' is not listed, so I can not get PID to use kill. I know I can use && instead of ; but it is too late now, I ran this command by accident on my server instead of my home PC and now I am trying to figure out how to prevent a disaster.

This problem is specific to rsync command followed by another command:

$ rsync ..... ; sudo shutdown -h now

Killing process by ID or killing it's parent will not work.

After several hours of testing, I isolated it to rsync, other commands I am able to terminate with CTRL+c in various orders, but not if they are after some commands like rsync or ping, I tried different Ubuntu versions, kernels, server without sudo user and I could reproduce it everywhere, best way to avoid this problem is to use && after rsync to execute following command only on success exitcode 0:

$ rsync .... && job 2
  • Edit your question an show the process tree. – A.B. Jun 29 '15 at 5:28
  • It would be great if one of the answers could explain what is special about rsync that prevents Ctrl-C from working as expected. – joeytwiddle Jun 29 '15 at 11:08

I think you miss something, When you run two commands seperated by ; you can kill the both commands by the CTRL+c.

just to be sure try this command:

find / -name a ; ls /home

Then kill the above commands with CTRL+c, this would kill both processes, you'll not list your home content. I.e, the next command will not run.

UPDATE: This also will be valid for your rsync command. Here my example:

$ rsync -avz /var/cache/apt/archives/ /tmp/backups/ ; sudo shutdown -h now

sending incremental file list
created directory /tmp/backups
^Crsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(549) [sender=3.0.9]
[sudo] password for user: 

So as you see above When you run CTRL+c, this kill the first command and while starting shudown it will ask you for your sudo password, so you can now kill the second process using CTRL+c again.

Update 2 As a hack for problem you can do:

  • since you use passwordless sudo then make again your sudo ask for password by editing your sudoer file sudo visudo
  • As a final step if nothing else works and just a hack and it's not advisable edit the /sbin/shutdown command to remove +x or just rename it to different name.
  • I have a rsync backup line followed by shutdown command: $ rsync ..... ; sudo shutdown -h now This for some reason does not end with Ctrl + C, I tried your example and it actually does list the home on Ctrl + C, I have bash version 4.2.25, but I read an article about an hour ago, this is supposed to be this way from bash2 onward. – Mike Jun 29 '15 at 5:40
  • I think you should edit your question and add the above comment since to answer in general is some how different from specific question. for sure this will not be used for your case – Maythux Jun 29 '15 at 5:42
  • @mike Also I have bash v 4.2.25 and ctrl+c stops the second command just the example above, already tested it once again – Maythux Jun 29 '15 at 5:48
  • Hi, for some reason after a restart your example works but I can not reproduce it on rsync followed by shutdown. – Mike Jun 29 '15 at 5:59
  • 3
    Another hack is to move the +x from /sbin/shutdown or rename the command so it will not be executed, then get the command back o default once you are in safe – Maythux Jun 29 '15 at 6:32

Kill the parent process of job1 with SIGKILL.

Show the processes in a process tree, eg. with htop or ps axf and kill the parent process of job1 and not job1 itself.

Install htop with

sudo apt-get install htop

Downvoters, understand the answer before downvoting

  • This will cause the disaster I was talking about, I tested this on my home PC, killing a process id will actually execute the second command, so this is not a good advice. – Mike Jun 29 '15 at 5:19
  • 1
    The parent process of job1 and not job1 – A.B. Jun 29 '15 at 5:24
  • @Mike This is valid solution also, What A.B means to kill the parent of job1 not job1 itself – Maythux Jun 29 '15 at 5:30
  • I just tried to kill a parent job just now again so to make sure I test your theory, it shutdown again, does this really work on your machine? Your answer was my first choice this morning when I found out, but from my previous experience I tried if first on my PC and the second command just gets executed. – Mike Jun 29 '15 at 5:53
  • 2
    Indeed, this works, BUT: you must kill parent process with SIGKILL, not with SIGTERM, otherwise it won't be killed, and the second job will execute after the first one ends. BTW, you don't need htop or any other additional packages to view process tree, a simple ps axf will do. – Ruslan Jun 29 '15 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.