How to automatically execute a python script with GUI on Ubuntu 18.04 after login, I was looking for a way to automatically execute the chmod command that I always used with sudo privileges after logging in. I found a way to work, so I wrote an article with a memorandum. I think it was important that the GUI came out.
Ubuntu18.04 -How to automatically execute a python script that creates a GUI after logging in -How to automatically execute a command with sudo authority after logging in
Ubuntu18.04 python3.8 anaconda environment use Set to automatically log in to a user after startup
-Search for "session" in the search window of the application list -Launch an application with a name like "Automatic application execution" -Press "Add", select the shell script you want to execute in the command field, and set it.
…that's all
session is also used to automatically start GUI applications such as firefox. ** It can be guaranteed that it will be executed after the GUI is ready to be launched on the OS side **, so I decided to run this python script that gives rise to the GUI here.
*** I also tried running it with crontab, but an error occurred when the GUI started up. ** ** When I register for a session, it takes time to execute, but That's why the OS needs to prepare.
/hoge/hoge/foo.sh
cd /home/user name/hoge/hoge
/home/user name/anaconda3/bin/python /home/user name/hoge/hoge/foo.py
Since the relative path was included in the python script I run I also created foo.sh in the same location as the script that runs it, For some reason for a file written with a relative path in a python script I got a file not found error, so I solved it by cd to the location of the executable file in the shell script.
I wrote the paths of python commands and scripts with absolute paths just in case.
Also, python uses the anaconda environment.
/etc/rc.local
#!/bin/sh
sudo chmod 666 /hoge/hoge
If rc.local does not exist, create it with sudo authority sudo vi /etc/rc.local
Make rc.local an executable file sudo chmod u+x /etc/rc.local
rc.local was /etc/rc.local in this environment.
From 18.04, systemd is used for autorun at startup, Rc.local is also available for compatibility.
Since rc.local is executed with root privileges, it is easy to write. (I haven't tried it, but I don't think it's necessary to write sudo) Since crontab does not have root privileges, writing the sudo command is a bit annoying.
I had a confusing impression when I tried to use systemd before. It didn't work very well (laughs), so I won't use systemd this time. However, in order to use rc.local in 18.04, you will have to run it from systemd after all.
Ubuntu 18.04 rc.localsystemd settings
It is necessary to add to the file called rc-local.service.
It usually exists under/bin/systemd/system, You have to be under/etc/systemd/system to have systemd run it, so Look at the points and choose the method.
If rc-local.service is not in/etc, create it or touch /etc/systemd/system/rc-local.service
Or originally /lib/systemd/system/rc-local.service, so please link from there ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
If you create a new one in/etc, execute the following command to enable it. systemctl enable rc-local.service
/bin/systemd/system/rc-local.service
# SPDX-License-Identifier: LGPL-2.1+
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
Documentation Ubuntu 18.04 rc.localsystemd settings (https://sourceexample.com/article/jp/1f2116cf8350c16df7cb34f970ebb76b/) Execute the command when the machine starts in an environment where rc.local cannot be used Use rc.local on ubuntu 18.04
When the GUI came out, the point was to register in session. Another point is that crontab and rc.local have different execution permissions. Files created with rc.local will have a key mark. There are various methods, but I hope it will be helpful for those who are in trouble in similar situations.
Recommended Posts