5

I have developed an application for the user to see certain times on screen. I am using the pygames library and the GUI is full screen after the user runs the .py file.

I want a solution so that the user does not need to run the file, but rather the file gets executed by itself after the log in is complete.

1

5 Answers 5

4

Add the script to the /etc/rc.local file.

On the Raspberry Pi, open /etc/rc.local with a text editor. I use Vim, with the sudo command.

Once you open you the file, you will see something like this:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# Add your script here
/home/pi/somescript.sh

exit 0

Once you've added the path to your script the file will be executed at reboot. If you want to run at login do the same thing, but edit your .bashrc file instead.

Sign up to request clarification or add additional context in comments.

4 Comments

This worked great for me to get udevadm trigger to run after boot.
Adding to .bashrc will also cause the script to be run each time a new shell is opened, not just on login.
This doesn't work the script is not visually run in the gui environment so you can't see the output unless you know how to find a running process and inspect it. I don't and I assume the OP doesn't either.
This can't possibly be an answer: rc.local does not run "when the user logs in", it runs when the pi boots up, well before anything login-related happens, and so runs regardless of who ends up logging in.
4

TLDR: Use the autostart directory.

Create a new entry for autostart...

cd /home/pi/.config/autostart/
sudo vim miner.desktop

In miner.desktop, I add the commands I want to run on start after the user has logged in. If you're unsure of the format, there usually is an example already available in that folder (for me, it was LXinput-setup.desktop), you can use that as a template, if desired.

[Desktop Entry]
Type=Application
Name=Miner (or whatever you want to call it)
Comment=MINE! (or whatever comment you want to add)
Exec=lxterminal --working-directory=/home/pi/Documents/miner/ -e ./start_moonlander2.sh
NotShowIn=GNOME;KDE;XFCE

As you can imagine, the value of Exec is what actually gets executed. Let's break down this command...

lxterminal --working-directory=/home/pi/Documents/miner/ -e ./start_moonlander2.sh
  • lxterminal : Application for starting a new process for a terminal window.
  • --working-directory=/home/pi/Documents/miner/ : Set the working directory of the execution. This will be very important if yourscript uses local files (i.e., if it's app assets are located in /pi/Documents/miner/, etc.).
  • -e ./start_moonlander2.sh : This is the command to run in the previously-indicated working directory. I'm executing a local sh script that's using assets from the working directory.

Want to know you did things half-way right? Your lxterminal command should run by itself if you run in a new terminal window. It'll cause a new terminal window to popup and run your command. If that crashes, then it's unlikely that this will boot correctly with a new [DeskTop Entry], and you'll need to get that working first.

Comments

1

Assuming the desktop is LXDE, LXSession can be used to automatically start application on login. To configure this: Menu -> Preferences -> Default applications for LXSession

Click on "Autostart" * Disable autostarted applications? = No

In the box next to "+Add" enter the name of the application and then press "+Add", it will appear under "Manual autostarted applications.

Setting up autostart for the application

Logout and log back in again, the application should start.

Autostarted application after login

Comments

1

For Raspberry Pi OS (rather than the older raspbian), first install the Default Applications for LXsession package, which used to be prebundled but now requires you to install it yourself:

sudo apt -y install lxsession-default-apps

Then, go to the the menu's "preferences", run "Main Menu Editor", select the preferences category on the left, and then enable "Default Applications for LXsession" so it shows up in the menu from now on:

editing the Raspberry Pi OS menu

Then run that, and go to "Autostart":

Editing the autostart settings for the currently logged in user

And then just add new entries for whatever you need to have run on user login and restart the pi for the changes to kick in.

However: this can only run applications "as you", so if you need to run commands that require sudo, you have two options:

  1. write a system.d service. Lots of great tutorials on this but it's a rabbit hole: good luck.
  2. "preclear" your specific script in /etc/sudoers with the more than obvious security implications. And if you mess up, you may need a (virtual machine'd) linux to mount your SD card and edit the sudoers file on a separate machine.

(while staying away from rc.local: that runs at part of the boot process, which means it's completely useless if you need something to happen when a specific user logs in).

And of course, remember that not everything needs sudo (for example, mounting a samba share as regular user just requires an fstab entry that includes ,user instead of forcing the issue using sudo, etc).

Comments

0

Raspberry Pi runs a Debian OS.

A related discussion on how to run Python scripts at startup can be found here.

If you want to run the script whenever a new terminal is launched, you need to update your .bash_profile, .profile, and .bashrc files. More on this is here.

3 Comments

all that is good but i need to script to be executed AFTER the login has been done
and preferably after i have opened up the desktop display
I'm trying to do the same thing. I want the script to run AFTER I login using putty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.