Sunday, May 5, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 1002  / 1 Year ago, tue, march 7, 2023, 11:50:05

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

More From » command-line

 Answers
2

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
./
authbind_1.2.0build3_amd64.deb
................
^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.


[#19332] Thursday, March 9, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dicatandyca

Total Points: 486
Total Questions: 108
Total Answers: 121

Location: Greenland
Member since Wed, Jan 18, 2023
1 Year ago
;