0

There are a lot of questions and answers about restarting Plasma 5. I realize that using KRunner is the best option, but today I could not access KRunner and had to use a terminal.

After reading Difference between nohup, disown and &, I felt like these two topics (restarting Plasma 5 and nohub/disown/background jobs) need to be combined into one answer specific to restarting KDE Plasma 5 the right way. Almost every answer I have seen about restarting Plasma 5 ignores the issue of nohup.

By following different answers about restarting Plasma 5, I have, at various times, found myself unable to close a terminal window without killing my newly started Plasma 5 session.

The following script is from a few answers, mostly https://unix.stackexchange.com/a/499373, and is modified to include nohup. Is this the definitive, comprehensive solution? Or is it a mess that needs to be avoided?

#!/bin/sh
kbuildsycoca5 # rebuilds the plasmashell database
timeout 5 kquitapp5 plasmashell #without timeout, it can hang for ~30-60 seconds
pgrep -U $USER -x plasmashell &>/dev/null && pkill -U $USER -x plasmashell
pgrep -U $USER -x plasmashell &>/dev/null && pkill -U $USER -x -9 plasmashell # here the process does not get to clean-up.
killall -9 plasmashell #sends a signal to all processes running any of the specified commands
pgrep -U $USER -x plasmashell &>/dev/null && echo "ERROR: cannot kill plasmashell"
nohup plasmashell &

My specific question is about the last line:

nohup plasmashell &

Is that correct in this context?

MountainX
  • 17,948
  • Not a nix veteran or something but it is my regular way to run anything from the terminal. – Abdullah Ibn Fulan Sep 26 '21 at 01:21
  • @AbdullahIbnFulan - thanks, but I'm hoping for an answer specific to plasmashell – MountainX Sep 26 '21 at 01:47
  • No, nohup is not the way to do anything sensible for a lot of reasons that have been rehashed ad-nauseam. Better try with setsid >/dev/null 2>&1 </dev/null your_command ... and be sure that systemd-logind is not configured to KillUserProcesses=yes –  Sep 26 '21 at 08:49
  • Of course, you can change >/dev/null to >/some/file if you want to capture the output of your_command. –  Sep 26 '21 at 08:57
  • @MountainX yes, including killall plasmashell && nohup plasmashell >/dev/null & – Abdullah Ibn Fulan Sep 26 '21 at 13:08
  • @UncleBilly - do you mind providing a link to a definitive answer RE "nohup is not the way to do anything sensible"? Or in regard to setsid being the solution? – MountainX Sep 26 '21 at 14:02
  • No problem, here is the definitive answer. –  Sep 27 '21 at 08:09
  • Seriously, even assuming that you did debug your issue and determined that plasmashell terminates because of a SIGHUP signal and not because of any other reason, nohup ... & is not reliable because it's racy and the background process could be killed by SIGHUP before being able to ignore the signal or exec its command. That will happen in 99% of cases where the nohup ... & is the last line. Since you can do the very same thing nohup does, but without the race with just (trap '' HUP; your_command ... &) there's exactly 0 reason to ever use nohup from a script. –  Sep 27 '21 at 08:13
  • But usually people use nohup as a kind of talisman, supposed to bring good luck to a process and shy away the spirits that may kill it along its journey. Millions of people doing that can't be wrong. –  Sep 27 '21 at 08:21

1 Answers1

0

Restarting kde apps as in the comments is the wrong way.

I have a script which I call if I wish to restart things such as plasmashell...

#!/bin/sh
for i in $* do
        kquitapp5 ${i}
        PID=$(ps -ax | grep ${i}$ | awk '{print $1}'
        test -n ${PID} && kill -9 ${PID}
        kstart5 ${i}
done
Bib
  • 2,380
  • Thanks for your answer. However, as discussed in various posts on restarting Plasma5, this method will not always work. Yesterday, for example, this method did not work for me. I could have rebooted, or used some version of kill -9 as in the OP. There may be other options, but this is not one of them in those circumstances. – MountainX Sep 26 '21 at 16:22
  • If you kill -9, then it should restart automatically. If not, then just run kstart5 plasmashell. You can add it to the script. It does work. – Bib Sep 26 '21 at 16:25
  • You can add it to the script. That's a low effort reply. There are already a lot of answers on other questions similar to this. My question seeks a more definitive solution (call it a canonical answer), particularly in regard to handling the harder cases and avoiding side effects. – MountainX Sep 26 '21 at 17:13
  • Satisfied now ? – Bib Sep 26 '21 at 17:31
  • thank you for editing your answer. It's not a bad answer now. However, this question has come up a lot in the past and I was specifically trying to avoid a rehash. After reading many prior answers, I remain unclear in regard to what is really the best answer. You have not provided convincing information that your answer really is better than any others. If I do not hold answers to this question to that higher standard, this question will simply devolve into a duplicate of all the others on this topic. – MountainX Sep 26 '21 at 18:01
  • The question asks specifically about nohup. My interest is in setsid, nohup, disown, etc. You did not address those. Maybe you don't need them. If not, could you explain why? In my experience, when I restart plasma by calling kstart5 plasmashell I have to keep that terminal open to keep plasma running. – MountainX Sep 26 '21 at 18:03
  • Use krunner then type it in.,.. You have no need whatsoever to start it from a terminal. kstart5 is the correct method. – Bib Sep 26 '21 at 20:54
  • Or bring up another terminal and run it in the background, then terminate the terminal. It still runs. – Bib Sep 26 '21 at 21:03
  • Use krunner then type it in.,.: you missed what I said: "I realize that using KRunner is the best option, but today I could not access KRunner." I find I have to use the terminal more often than not because when Plasma is having trouble, KRunner is also. Fortunately, Plasma5 is very stable for me. But, as I said, I am not looking for a rehashed answer like those already out there. I asked the question in order to focus on using nohup/setsid/disown in a terminal. – MountainX Sep 27 '21 at 00:11
  • Or bring up another terminal and run it in the background, then terminate the terminal. It still runs. That's closer to the issue I am asking about but the correct answer should focus on what is the best way to "background it": nohup, diwown, setsid, etc. and explain why. Thanks anyway. – MountainX Sep 27 '21 at 00:13
  • If you cannot access krunner and it is not functioning, then restart it. Seriously, why are you making it so difficult for yourself. The script I gave can restart both and many other kde programs. – Bib Sep 27 '21 at 07:58