0

I have a raspberry pi, which is online and connectable by SSH.

What I want to do is create hooks for the following states:

  • When the device starts
  • Before the device shuts down
  • When an SSH login takes place

This is done as a basic security measure.

My script will simply send a message to various channels, like Slack and Pushbullet, with information like the event that triggered it, current logged-in user and a timestamp.

How do I get my python script to run, when these events take place? I thought about putting it in ~/.bashrc, but that will run the script whenever the profile is sourced.

polym
  • 10,672
  • 9
  • 41
  • 65
sparkonhdfs
  • 111
  • 1
  • 1
    @Munir not a duplicate, if the title is changed to what the user really wants: Find out what type of hooks exist for several use cases (SSH Login, Device Bootup/Shutdown etc.) – polym Apr 11 '16 at 16:58
  • 1
    @polym That was not the question when I marked it as a duplicate. It got edited later I guess. In fact, this is a completely different question from what it originally was. – Munir Apr 11 '16 at 17:50
  • 1
    @Munir since I edited the question by reformatting the text and just updating the title, I have to disagree. On first glance, the title did suggest a duplicae. Reading the (not altered) description text, you find that the user did indeed ask for more. And this discussion reminds me why I did forfeit helping here. – polym Apr 11 '16 at 21:17

1 Answers1

1

For:

  • When the device starts
  • Before the device shuts down

You need to look at an init script. How you would implement this depends on what linux/unix distribution you are using. Do you know which one? You can usually find out by typing the following at the command prompt:

cat /etc/*release

If you are wanting to execute a script on SSH Login you want to place the script in ~/.bash_profile (~/.bashrc runs on non-interactive logins as well, which you may not want). This will need to be done on every account you wish to monitor. You can also place it in the system wide profile, but this is distribution specific as well.

  • I am using rasbian. The problem with placing it in bash_profile is that sourcing the file is not exclusive to a startup/login event. – sparkonhdfs Apr 11 '16 at 22:08
  • 1
    I did some research, and found out about the different user levels in Debian (https://wiki.debian.org/RunLevel), so I assume that I can place the shutdown script in `rc0.d`, and reboot one in `rc6.d`. The startup file can go as an `init.d` file. Are these correct? I am still not sure where to place a script for SSH login though. – sparkonhdfs Apr 11 '16 at 22:10
  • Section 3.2.1 of the following document describes what runlevels do what: https://www.debian.org/doc/manuals/debian-reference/ch03.en.html#_stage_4_the_normal_debian_system You are correct, the startup file will go in init.d, and you will create links to this file in the respective runlevel directory. Could you elaborate on "problem with placing it in bash_profile is that sourcing the file is not exclusive to a startup/login event" This will catch logins via ssh and on the local console, and adding a startup script to init.d as describe earlier will catch on boot/shutdown – mystupidquestion Apr 12 '16 at 13:15
  • The problem is that the bash_profile is sourced whenever a new terminal window is launched in Linux. I currently don't use X, but if I were to start it, and launch instances of terminals, it would source the bash_profile, and send me a notification whenever I do that. It's not exclusive to a login event. – sparkonhdfs Apr 13 '16 at 19:09