How to automatically execute python script (with GUI) and sudo permission command after logging in with Ubuntu 18.04

Introduction

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.

What you can understand by reading this article

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

Execution environment

Ubuntu18.04 python3.8 anaconda environment use Set to automatically log in to a user after startup

Implementation content

How to automatically execute Python scripts

Register a shell script that executes a python script in session

-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.

By the way, the contents of .sh

/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.

How to execute a command with sudo privileges

Describe the command in rc.local

/etc/rc.local


#!/bin/sh
sudo chmod 666 /hoge/hoge

point

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

Explanation

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.

reference

Ubuntu 18.04 rc.localsystemd settings

Preparations required to run rc.local

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.

point

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

Additions to 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

reference

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

in conclusion

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

How to automatically execute python script (with GUI) and sudo permission command after logging in with Ubuntu 18.04
How to log in to AtCoder with Python and submit automatically
How to execute a command using subprocess in Python
[Linux] After connecting to DB with teraterm and typing Sqlplus command, until logging in to DB (oracle)
How to use Python with Jw_cad (Part 2 Command explanation and operation)
Try logging in to qiita with Python
How to work with BigQuery in Python
[Ubuntu] How to execute a shell script
How to use is and == in Python
How to get the date and time difference in seconds with python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to generate permutations in Python and C ++
How to plot autocorrelation and partial autocorrelation in python
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
[For beginners] How to use say command in python!
How to convert / restore a string with [] in python
Periodically execute python script with cron in venv environment
How to add help to HDA (with Python script bonus)
Explain in detail how to make sounds with python
How to run tests in bulk with Python unittest
[Note] How to write QR code and description in the same image with python
[Python] How to sort dict in list and instance in list
It is easy to execute SQL with Python and output the result in Excel
Script to count and stop up to 5 seconds in Python in Blender
Try to make it using GUI and PyQt in Python
How to swap elements in an array in Python, and how to reverse an array.
How to extract any appointment in Google Calendar with Python
[Python beginner] How do I develop and execute Python after all?
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Comparison of how to use higher-order functions in Python 2 and 3
I tried to make GUI tic-tac-toe with Python and Tkinter
How to create dataframes and mess with elements in pandas
Install CaboCha in Ubuntu environment and call it with Python.
Execute external command in python
How to develop in Python
[Python] What is pip? Explain the command list and how to use it with actual examples
How to deal with python installation error in pyenv (BUILD FAILED)
How to not escape Japanese when dealing with json in python
How to install OpenCV on Cloud9 and run it in Python
How to pass arguments to a Python script in SPSS Modeler Batch
How to change Django's SQLite3 uploaded to python anywhere with GUI only
How to import CSV and TSV files into SQLite with Python
How to use functions in separate files Perl and Python versions
How to make a surveillance camera (Security Camera) with Opencv and Python
How to get a string from a command line argument in python
How to create a heatmap with an arbitrary domain in Python
How to use python put in pyenv on macOS with PyCall
Difference in how to write if statement between ruby ​​and python
How to deal with errors when installing Python and pip with choco
[ROS2] How to describe remap and parameter in python format launch
Throw something to Kinesis with python and make sure it's in
How to automatically install Chrome Driver for Chrome version with Python + Selenium + Chrome
How to display legend marks in one with Python 2D plot
How to build Python and Jupyter execution environment with VS Code
How to calculate "xx time" in one shot with Python timedelta
How to display bytes in the same way in Java and Python
Python: How to use async with
How to collect images in Python
How to use SQLite in Python
In the python command python points to python3.8