How Should I Use Trap with su Command to Avoid Ctrl-C Interruptions?

0
3
Asked By CuriousCat99 On

I'm trying to prevent Ctrl-C from interrupting a command I want to run in the terminal using `su - -c`. Should I use the command as `su - -c 'trap "" INT; some_command'` or `trap '' INT; su - -c 'some_command'; trap - INT`? What are the differences between these two methods?

1 Answer

Answered By TechGuru42 On

It might be better to put the trap inside the subshell using the first approach. It seems cleaner semantically, and according to the `su` documentation, when you switch to superuser, `su` automatically blocks SIGINT and SIGQUIT. That way, you won’t have to worry about those signals affecting the `su` process itself.

SignalSleuth87 -

Are you saying that with `su -`, those signals don't affect the new root shell? And when using `-c`, they are not blocked? That fits my experience! Also, does the pre-existing trap still affect the subshell if it's under a different user?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.