You are not logged in.
How can I continue to run a bash script after stopping display manager?
Last edited by laggykiller (2020-04-12 16:39:08)
Offline
One has absolutely nothing to do with the other.
Describe the actual problem you are having - what are you currently doing? Presumably, you are launching some shell script. How are you currently launching it?
If you run a shell script as your logged in user, then log out of that user session, child processes will be killed (unless launched with nohup, setsid, or other mechanisms appropriate to the circumstances). But this has nothing to do with the display manager as the display manager runs as root, not your user.
Last edited by Trilby (2020-04-12 15:20:24)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Offline
I am writing a bash script that switch on/off discrete Nvidia graphics card without powering off the computer. To power off the Nvidia graphics card, I have to first stop display manager, then sudo modprobe -r nvidia, followed by sudo tee /proc/acpi/bbswitch <<< OFF, finally starting display manager again.
The bash script is launched from lxhotkey or lxterminal.
Last edited by laggykiller (2020-04-12 15:40:57)
Offline
Or in simple words, I want to make a bash script, launched from terminal emulator, which stops display manager, then run a command, finally starts display manager again.
Offline
I don't use display managers, but I gather that stopping the display manager logs you out of your current session, right?
First, do you actually need to stop the display manager? Why not just do the other steps, then restart the display manager. This would likely work even when run as a user.
However, the best solution would be for this script to run as root in the first place. You are - apparently - running 'sudo' in the script which doesn't make any sense if stopping the display manager logs you out: if you are not logged in, you cannot interactively provide your password.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Yes, stopping display manager logs out current session and stop Xorg.
Unfortunately, desktop environment must be stopped before removing nvidia kernel module and powering off Nvidia graphics card, so I must find a way to continue the script after stopping display manager. It seems that for my setup, it is only possible to stop desktop environment by stopping display manager with systemctl.
Running the script as root in terminal emulator with 'sudo myscript.sh' still results in the same problem of the script getting killed after stopping display manager.
Offline
You'd still need nohup and/or setsid to disown the script. The best approach may be to run it from a service file, then you just start your new service to swtich between w/nvidia and without.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Tried nohup before but does not work.
I tried to run the script from service file. This method sounds hacky, but it works! Thank you very much.
Offline
Tried nohup before but does not work.
Tried how exactly?
nohup exists for one specific purpose: this one.
You can run something from a shell, then close that shell and nothing happens to the other job anymore.
And arch re-defaults systemd's imbecilic "let's just kill everything when the user logs out by default" (KillUserProcesses in logind.conf)
Offline
nohup sh myscript.sh &
From https://superuser.com/questions/478182/ … ssion-xorg
You run this script from a terminal emulator that is itself running within the LXDE session. When you kill the LXDE session, you also kill the terminal and, therefore, your script.
Also, I found that nohup is usually used in keeping script alive after closing ssh session...?
Anyway, from experiments, the nohup command does not work as intended for me. Just for the sake of learning, is there an explanation, or a correct way to do it?
Offline
If you're still using 'sudo' within the script, that would definitely fail. The nohup would work, and the script would run just fine (and not be killed by the DM shutting down) but the script wouldn't do what you want because having the sudo commands in the script was a bad idea from the beginning. You are not able to enter your password interactively for the script, so sudo either times out or just fails immediately as it is not connected to a controlling tty/pty.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Here is my test for nohup:
$ cat mytestscript.sh
#!/bin/sh
systemctl stop lightdm
touch ./itworks
systemctl start lightdmTest 1:
$ sudo nohup sh mytestscript.sh &
[1] 3284
* Crickets chirp$ ls
mytestscript.sh nohup.outTest 2:
$ sudo nohup sh mytestscript.sh
[sudo] password for user:
* Enters my password and enter
* Screen goes black, indicating lightdm has been stopped
* Screen never goes back to login prompt, indicating lightdm cannot be started$ ls
mytestscript.sh nohup.outSo it seems like it does not work or I am doing it wrong
As for using sudo within the script, the reason is I usually launch the script via lxhotkey, and I don't want to enter the password before launching the script. So what I did is add the script into /etc/sudoers, then set up lxhotkey so that when I press a certain key, it triggers sudo myworkingscript.sh. This works for me.
EDIT: Just now tried to remove sudo inside myworkingscript.sh while keeping the myworkingscript.sh in /etc/sudoers, and myworkingscript.sh works fine either way. However, if I sudo inside the script, I can still run the script in terminal emulator if I forgot to start it with sudo (Though I have to enter password once)
Last edited by laggykiller (2020-04-13 04:29:26)
Offline
As for using sudo within the script, the reason is I usually launch the script via lxhotkey, and I don't want to enter the password before launching the script. So what I did is add the script into /etc/sudoers, then set up lxhotkey so that when I press a certain key, it triggers sudo myworkingscript.sh. This works for me.
And in this case you should not have sudo in the script as the script is already running as root. It's likely harmless, but also completely pointless.
In any case, the reason the nohup is failing is likely due to the DM sending SIGTERM/SIGKILL to child processes which is why in my first comment in this thread I suggested nohup and/or setsid; nohup only ignores SIGHUP, TERM/KILL still are propogated. Setsid can sidestep these as well. But combining sudo, setsid, and potentially nohup (and then additionally backgrounding with an ampersand likely for no reason) just gets to be a confusing mess which is why I also suggested from the start that this was not really a good approach: a systemd service is much cleaner.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
combining sudo, setsid, and potentially nohup
Sounds too complicated to me too! So I gonna stick with using service.
And in this case you should not have sudo in the script as the script is already running as root. It's likely harmless, but also completely pointless.
Though the benefit of sudo inside the script is I can still run the script in terminal emulator if I forgot to start it with sudo (Though I have to enter password once), the drawback is ugly code. Gonna remove sudo in my custom scripts then ![]()
Thanks for helping!
Offline