LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-23-2020, 09:31 AM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 327

Rep: Reputation: 54
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
 
Old 07-23-2020, 09:56 AM   #2
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,621

Rep: Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619
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!
 
1 members found this post helpful.
Old 07-23-2020, 09:59 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 6,120

Rep: Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901
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.
 
1 members found this post helpful.
Old 07-23-2020, 11:04 AM   #4
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 327

Original Poster
Rep: Reputation: 54
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?
 
Old 07-23-2020, 11:21 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 16,858
Blog Entries: 10

Rep: Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013
Quote:
Originally Posted by ddenial View Post
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.
 
3 members found this post helpful.
Old 07-23-2020, 11:33 AM   #6
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 327

Original Poster
Rep: Reputation: 54
Quote:
Originally Posted by ondoho View Post
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.
Attached Thumbnails
Click image for larger version

Name:	Screenshot from 2020-07-23 21-01-38.png
Views:	14
Size:	156.5 KB
ID:	33695  
 
Old 07-23-2020, 11:38 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 16,858
Blog Entries: 10

Rep: Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013
No, point it to asz.sh instead.
 
Old 07-23-2020, 12:06 PM   #8
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 327

Original Poster
Rep: Reputation: 54
Tried. Same result.

Have attached three screenshots. The last one is what I want to achieve from the script.
Attached Thumbnails
Click image for larger version

Name:	AutoStart.png
Views:	13
Size:	166.8 KB
ID:	33697   Click image for larger version

Name:	Before.png
Views:	11
Size:	168.9 KB
ID:	33698   Click image for larger version

Name:	After.png
Views:	13
Size:	178.9 KB
ID:	33699  
 
Old 07-24-2020, 02:54 AM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 1,832

Rep: Reputation: Disabled
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
 
2 members found this post helpful.
Old 07-24-2020, 09:00 AM   #10
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 327

Original Poster
Rep: Reputation: 54
Quote:
Originally Posted by shruggy View Post
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
 
1 members found this post helpful.
Old 07-24-2020, 03:45 PM   #11
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Older: Coherent, MacOS, Red Hat, Big Iron IXs: AIX, Solaris, Tru64
Posts: 2,326

Rep: Reputation: 438Reputation: 438Reputation: 438Reputation: 438Reputation: 438
Quote:
Originally Posted by berndbausch View Post
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.
 
Old 07-24-2020, 04:26 PM   #12
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 16,858
Blog Entries: 10

Rep: Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013Reputation: 5013
Quote:
Originally Posted by rnturn View Post
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 View Post
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.
 
Old 07-25-2020, 10:13 AM   #13
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Older: Coherent, MacOS, Red Hat, Big Iron IXs: AIX, Solaris, Tru64
Posts: 2,326

Rep: Reputation: 438Reputation: 438Reputation: 438Reputation: 438Reputation: 438
Quote:
Originally Posted by ondoho View Post
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the minimal and mandatory content loaded into ram from rootfs on boot up after kernel loaded? ayyasprings Linux - Kernel 3 03-22-2019 03:15 PM
how to take permission to execute root privilege command like iptables in web GUI kikilinux Programming 4 01-23-2016 06:52 AM
Execute a command , reboot and then execute another command ganeshp@moris.org Linux - Newbie 3 12-03-2008 01:51 AM
Dynamically loaded modules and statically loaded modules in linux gauthamk Linux - Software 1 05-18-2008 05:28 PM
paraport_pc module loaded with wrong options, st not loaded adrianmariano Debian 2 12-18-2004 10:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration