LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to execute a command when GUI is loaded? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-execute-a-command-when-gui-is-loaded-4175679226/)

ddenial 07-23-2020 08:31 AM

How to execute a command when GUI is loaded?
 
Hello All

How do you run a command/script after the Linux is booted and GUI is loaded?

I have this script that should run once after every boot when GUI is loaded
Code:

$ cat asz.sh
#!/bin/bash
SCREEN=$(xrandr | grep primary | awk '{print $1}')
xrandr --output $SCREEN --auto

I tried in crontab with @reboot, but doesn't work as the GUI has not yet loaded and the 'Virtual-?' number is not yet assigned.

I put in /etc/bash.bashrc, but nothing happens. But it will execute as soon as I open terminal window. I don't want this to happen.

So where do I put the script which makes it executed after the GUI is loaded?

The distro is Kali Linux XFCE in KVM as a virtual guest.

Thanks

wpeckham 07-23-2020 08:56 AM

I have never needed anything to run one time and GUI startup, but if I did I would dig into the details on configuring XFCE!

berndbausch 07-23-2020 08:59 AM

Since Kali is based on Debian, it should use systemd. The rc-local service is executed as the very last service; try adding your script to /etc/rc.local and ensure this service is enabled.

Or create a service unit that launches the script and runs after the graphical target.

ddenial 07-23-2020 10:04 AM

Did the following

Code:

$ ls -lh /etc/systemd/system/ixrandr.service
-rw-rw-r-- 1 root root 155 Jul 23 20:25 /etc/systemd/system/ixrandr.service

$ cat /etc/systemd/system/ixrandr.service
[Unit]
Description=Auto resize KVM guest by running xrandr

[Service]
Environment="DISPLAY=:0"
ExecStart=/root/asz.sh

[Install]
WantedBy=graphical.target


$ sudo ls -lh /root/asz.sh
-rwxr-xr-x 1 root root 94 Jul 23 20:15 /root/asz.sh

$ sudo cat /root/asz.sh
#!/bin/bash
SCREEN=$(xrandr | grep primary | awk '{print $1}')
xrandr --output $SCREEN --auto


$ sudo systemctl enable ixrandr.service

$ sudo reboot

$ sudo systemctl status ixrandr.service
● ixrandr.service - Auto resize KVM guest by running xrandr
    Loaded: loaded (/etc/systemd/system/ixrandr.service; enabled; vendor preset: disabled)
    Active: failed (Result: exit-code) since Thu 2020-07-23 20:28:02 IST; 4min 12s ago
    Process: 522 ExecStart=/root/asz.sh (code=exited, status=1/FAILURE)
  Main PID: 522 (code=exited, status=1/FAILURE)

Jul 23 20:28:00 kali systemd[1]: Started Auto resize KVM guest by running xrandr.
Jul 23 20:28:02 kali asz.sh[550]: Can't open display :0
Jul 23 20:28:02 kali asz.sh[582]: Can't open display :0
Jul 23 20:28:02 kali systemd[1]: ixrandr.service: Main process exited, code=exited, status=1/FAILURE
Jul 23 20:28:02 kali systemd[1]: ixrandr.service: Failed with result 'exit-code'.

What I'm doing wrong?

ondoho 07-23-2020 10:21 AM

Quote:

Originally Posted by ddenial (Post 6148481)
What I'm doing wrong?

You can't do this as root and you can't do it before $DISPLAY is defined, i.e. before the GUI is up.

I'm pretty sure XFCE has a setting for "Startup Applications" or "Autostart", where you can add your own scripts and commands. That's what you want.

ddenial 07-23-2020 10:33 AM

1 Attachment(s)
Quote:

Originally Posted by ondoho (Post 6148494)
You can't do this as root and you can't do it before $DISPLAY is defined, i.e. before the GUI is up.

I'm pretty sure XFCE has a setting for "Startup Applications" or "Autostart", where you can add your own scripts and commands. That's what you want.

Unfortunately not working. :( Tried for both script and command directly.

ondoho 07-23-2020 10:38 AM

No, point it to asz.sh instead.

ddenial 07-23-2020 11:06 AM

3 Attachment(s)
Tried. Same result.

Have attached three screenshots. The last one is what I want to achieve from the script.

shruggy 07-24-2020 01:54 AM

Then do it manually. Create a .desktop file and put it to ~/.config/autostart/
Code:

[Desktop Entry]
Encoding=UTF-8
Name=asz
Exec=/usr/local/bin/azs.sh
Terminal=true
Type=Application


ddenial 07-24-2020 08:00 AM

Quote:

Originally Posted by shruggy (Post 6148712)
Then do it manually. Create a .desktop file and put it to ~/.config/autostart/
Code:

[Desktop Entry]
Encoding=UTF-8
Name=asz
Exec=/usr/local/bin/azs.sh
Terminal=true
Type=Application


This one worked. Great!!! Thanks

rnturn 07-24-2020 02:45 PM

Quote:

Originally Posted by berndbausch (Post 6148438)
Since Kali is based on Debian, it should use systemd. The rc-local service is executed as the very last service; try adding your script to /etc/rc.local and ensure this service is enabled.

That's what the Systemd people claim but I've found this to not be true. At least it doesn't seem to run last on OpenSUSE---it runs after "basic.target" (i.e., it's not the equivalent of the traditional "S99rclocal" SysV startup step) so I cannot fathom what the Systemd folks envisioned users would use that service for. There are some elaborate HOWTOs that I've tinkered with that try to fix this by adding a new boot target that multiuser or graphical are predecessors to. None worked for what I was looking to accomplish. IMHO, Systemd will not be done until rc-local is fixed. Desktop environments have a workaround for this (autostarting an application upon login) but plain ol' servers are left high and dry.

ondoho 07-24-2020 03:26 PM

Quote:

Originally Posted by rnturn (Post 6148950)
That's what the Systemd people claim but I've found this to not be true. At least it doesn't seem to run last on OpenSUSE---it runs after "basic.target" (i.e., it's not the equivalent of the traditional "S99rclocal" SysV startup step) so I cannot fathom what the Systemd folks envisioned users would use that service for. There are some elaborate HOWTOs that I've tinkered with that try to fix this by adding a new boot target that multiuser or graphical are predecessors to. None worked for what I was looking to accomplish. IMHO, Systemd will not be done until rc-local is fixed. Desktop environments have a workaround for this (autostarting an application upon login) but plain ol' servers are left high and dry.

I can neither confirm nor deny, but it is a moot point in this context:
Quote:

Originally Posted by ondoho (Post 6148494)
You can't do this (...) before $DISPLAY is defined, i.e. before the GUI is up.

So the "DE workaround" you mention is the correct way in this case.

rnturn 07-25-2020 09:13 AM

Quote:

Originally Posted by ondoho (Post 6148969)
I can neither confirm nor deny, but it is a moot point in this context:

So the "DE workaround" you mention is the correct way in this case.

Yes: in this case. Even if rc-local was working as it traditionally has, it might not have solved the OP's problem. Was it OK for the task to run after the GUI login was presented? Or did it need to wait for when the user completed logging in? (My guess would be the latter.)

Hmm... ~/.xsession might also solve the OP's problem and also be desktop agnostic. We tend forget about the Old Ways.

Cheers...


All times are GMT -5. The time now is 04:56 AM.