2

At the minute, I have an extra line in the ~/.profile file to start a program when my Pi boots up, is there a way I can update the script to detect if it's activated locally or from an SSH connection?

I need to be able to log in remotely using the same account, but don't want the program running a second time, especially over SSH.

Thanks

closed as off-topic by Ghanima, mpromonet, ArchHaskeller, Phil B., Milliways Oct 21 '15 at 22:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – Ghanima, mpromonet, ArchHaskeller, Phil B., Milliways
If this question can be reworded to fit the rules in the help center, please edit the question.

  • For posterity, please note that putting something in ~/.profile is probably not how you want to start something at boot, and unless your system is set up to autologin a user (which is probably a bad, and pointless, idea on a headless system), it just plain will not work either. I.e., don't do this unless you have a good reason. For a better understanding of the whys and wherefores, please read "INVOCATION" in man bash. If you want to run something at boot, put it in /etc/rc.local. – goldilocks Oct 19 '15 at 11:58
  • It is an autologin user, and whilst the pi is generally left headless, the program does have display feedback and a certain level of interaction, which I don't think you'd be able to get running from /etc/rc.local – Marq Watkin Oct 19 '15 at 12:05
  • It might if you set it up properly -- but I'm not actually telling you to change anything. If this works for you, great. All rules have exceptions (and of course, that's sort of what "hacking" is about, bravo). I simply put that there because in general this is the wrong way to go about starting something at boot (which can be made to work because of the unorthodox autologin option used on Raspbian). I just saw this and thought, "Please, please don't let other people read this and cargo cult it because it sounds easy." – goldilocks Oct 19 '15 at 12:16
2

You can use SSH_CONNECTION environment variable, which is set if you connect over ssh. It works on my Linux machine, but I believe it will work also in Pi. Simple script can look like this:

if [ ! -z "$SSH_CONNECTION" ]; then
  echo "Connected over ssh from $SSH_CONNECTION"
fi
2

As the question seems to be twofold: a) check if run locally or via ssh and b) prevent to run twice, let me add a little bit about b) to Jakuje's answer how check for a).

PID and lock files are a quite common approach to this issue on Unix systems to ensure that a program runs only once. Usually those files are placed in /var/run/ or /var/lock/. First thing your script does is to check for the existence of these files. If they do that would mean that it's already running or has been exited disgracefully (aka crashed) - which is obviously the main drawback of this otherwise simple approach. If the files do not exist, your script writes it's PID and/or a lock file and normally proceeds to do whatever it is meant to do. Upon exit it should clean up PID and lock file.

See here for more advice.

Not the answer you're looking for? Browse other questions tagged or ask your own question.