1

Let's say I used raspi-config to set my raspberry pi to auto-login to the 'pi' user. How would I have something run after login? My desired effect is that I plug in the pi, and without giving it any input, it boots to a desktop and then runs a shell script. I don't want the script to run before the auto-login - so I wouldn't want to put it in /etc/init.d

Jacobm001
  • 11,898
  • 7
  • 46
  • 56
Charles Noon
  • 113
  • 1
  • 1
  • 4

2 Answers2

2

Add this:

@reboot /path/to/python action.py

in your crontab. So, whenever you start your PI, it will automatically call your python script. Plus, this cron job should belong to your same pi user which is auto login in, otherwise it won't get called.

Gaurav Dave
  • 200
  • 1
  • 2
  • 15
  • Yes, I've tried this, and it works. Thank you! You may want to change 'restart' to 'reboot' in your answer, though, as that is the actual command. Also, I don't know if this will apply to other cases, but I had to do sudo crontab -e and have the root crontab run my script. – Charles Noon Apr 04 '16 at 04:14
  • @CharlesNoon edited my answer. – Gaurav Dave Apr 04 '16 at 04:50
0

~/.bashrc is the common way, I guess.

Jacobm001
  • 11,898
  • 7
  • 46
  • 56
PaulF8080
  • 307
  • 1
  • 7
  • 1
    .bashrc is a poor choice as it will be run every time the user opens a terminal window. – Jacobm001 Mar 16 '16 at 15:15
  • So you are saying he meant the first login in his title? bashrc is s login script so I took his title literally. – PaulF8080 Mar 16 '16 at 16:39
  • 1
    .bashrc is not just a login script. It runs every time bash is started. It sounds like the OP wants a script to run when the machine starts, and then logs in. .bashrc will run every time the bash interpreter is started. Which means it's happening a lot more often than when the user logs in. – Jacobm001 Mar 16 '16 at 16:40
  • When you open a terminal you logon to the terminal. I got docked 2 points because you don't know what a login script is. Haven't you ever seen the messages where user x logs onto TTY a and TTY b etc. – PaulF8080 Mar 16 '16 at 17:27
  • The user is asking about a desktop interface. .bashrc will run whenever a bash prompt is started. It's fundamnetally different than what the user asked for. https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29#When_started_as_an_interactive_login_shell – Jacobm001 Mar 16 '16 at 18:04
  • Yeah, this isn't really what I'm looking for. This would only run after I started a terminal emulator, if I booted to desktop. Thanks for the suggestion, though! – Charles Noon Mar 18 '16 at 19:36
  • I runs when you login – PaulF8080 Mar 20 '16 at 17:53