I recently got myself a Raspberry Pi 2 to learn a couple of things in my spare time. It is now running on Raspbian and I control it remotely via ssh from a laptop with Linux Mint 17.2 installed.

Now I would like the Pi to automatically tell the laptop that it is online after a reboot so that I know that I can connect to it via ssh. I know I can just wait a few seconds or ping the Pi, but somehow I got it into my head that it would be nice if a small message popped up in my terminal on the laptop.

What I got so far after some tinkering is the following (I'm VERY new to this, so I'm not even aware of the levels this might be horribly wrong on):

/usr/bin/ssh 'laptop_user'@'laptop_ip' "echo '### RaspberryPi 2 online ###' | /usr/bin/write 'laptop_user' pts/0"

This works when run in a terminal on the Pi if my laptop has the IP 'laptop_ip' and if 'laptop_user' is logged in on pts/0 (lots of if's, but I figured I would get to those after I got the initial idea up and running). On the laptop terminal something like

Message from 'laptop_user'@'laptop_host' on pts/0 at 09:58:
### RaspberryPi 2 online ###

appears. (yeah!)

I then put the command into a small script:

#! /bin/sh
/usr/bin/ssh 'laptop_user'@'laptop_ip' "echo '### RaspberryPi 2 online ###' | /usr/bin/write 'laptop_user' pts/0"
exit 0

saved it as /etc/network/if-up.d/sayhi on the Pi, and made it executable (following the best answer on this question). I checked that this script does indeed get executed after each reboot of the Pi. The thing is, if I run the script manually everything works fine and I get the message on my laptop terminal. But if the script is automatically executed on reboot I don't get the message. Putting the command into rc.local or crontab didn't work either.

I unfortunately lack the knowledge of how a startup of the Pi (or any computer) actually works. So I don't know if the services required for this command are already good to go.

So my question is:

Why don't I get the "online" message when the script is run automatically and when should I run my little script to achieve the desired behavior.

Also, there might be way better alternatives to my way of doing this. So if anyone could point me in the right direction I'd appreciate it.

Thanks in advance!


  • I forgot to mention that I'm using key authentication and as the script will be run as the Pi's root I added its public RSA key to authorized_keys on the laptop and I'm using the private key as the identify file for the ssh command.

  • I'm now logging the output of /sbin/ip addr while running the script on startup and if gives me:

    eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether b8:27:eb:34:66:ce brd ff:ff:ff:ff:ff:ff

    When running the script later (manually over ssh) /sbin/ip addr gives me:

    eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether b8:27:eb:34:66:ce brd ff:ff:ff:ff:ff:ff inet brd scope global eth0 valid_lft forever preferred_lft forever

    So the problem seems to be that the Pi does not have a local IP while running the scripts in /etc/network/if-up.d. I now have to run my script after the IP is assigned. Unfortunately, I don't know enough about networking to be able to do so.

  • Hey, Welcome to UnixSE. Feel free to take a tour(unix.stackexchange.com/tour) of the community. You mentioned you're monitoring from laptop with Mint. So, I'd rather send a desktop notification with notify-send than write. But, maybe that's because I haven't used write much. – Bibek_G Jan 6 '16 at 10:56
  • 1
    I'd say this is because startup scripts are run as root and you are using your local public key as login with your normal user? You need to add the /root/.ssh/id_rsa.pub as trusted if so is the case. I can write an answer in a few min explaining it but I'm on the phone now – Torxed Jan 6 '16 at 12:10
  • @Bibek_G That looks exactly like the thing I'm looking for. It won't resolve the network/script issue (I guess) but I will definitely look into this. Thanks! – nieswand Jan 6 '16 at 13:32
  • @Torxed I think I get it and you might very well be right. Will test this as soon as possible. Thanks! – nieswand Jan 6 '16 at 13:34
  • Which user's crontab did you use? How do you authenticate on your laptop with SSH: do you use a key? Does it have a password? – Gilles 'SO- stop being evil' Jan 6 '16 at 23:19

Most likely this is because your startup script will run in a root environment by default. Assuming that you're using ~/.ssh/id_rsa.pub as your mode of authentication (You never mentioned that you're using a password and using such a thing while automate is often a bad idea anyway, so i'll assume key authentication).

Then I'll go ahead and assume even further that you haven't allowed the (or even have a) root key generated that is trusted on your laptop?

You have two options in this case.

  1. run ssh-keygen as root, and copy the content of /root/.ssh/id_rsa.pub to your /home/<user>/.ssh/authorized_keys file.


  1. change your command to /usr/bin/ssh -i /home/<user>/.ssh/id_rsa.pub 'laptop_user'@'laptop_ip' "echo '### RaspberryPi 2 online ###' | /usr/bin/write 'laptop_user' pts/0"

The second is neater since it uses your users certificate that you already know works.

debian startup order and network connectivity issue

Most likely (after reading your comments) this is a script startup order issue, meaning that your script is run betwork network.d has had a chance to DHCP your interface and bring it up.

even rc.local is run after network.target but that's not the same as network-online.target sorry to say.

You have again a few options here, one is to simply add this to your crontab line:

@reboot sleep 60 && /usr/bin/ssh -i /home/<user>/.ssh/id_rsa.pub 'laptop_user'@'laptop_ip' "echo '### RaspberryPi 2 online ###' | /usr/bin/write 'laptop_user' pts/0"

Which will sleep your command for 60 seconds before executing the SSH command.
It's not the most pretty thing in the world, but if you don't care about real time "notifications" go with it, it's quick and it works.

If you want a more reliable options tho, i'd suggest you create a init.d script with a target requirement for network-online.target which won't trigger your init script until the network is online. This is the most fastest and reliable option to go with.

I use systemd so I can't write or verify a proper init.d script atm, try following this guide and see if it works: https://www.debian-administration.org/article/28/Making_scripts_run_at_boot_time_with_Debian

  • Unfortunately, neither of those options works. Also: the identify file (-i) should be the private key, right? – nieswand Jan 6 '16 at 21:41
  • @nieswand You are correct, that should be the private file! Humm, this is most peculiar. under which crontab -e are you doing this? Can you, in your script do whoami > ~/whoami.log and check that file for the users name? Are you sure this script is being run after your have your network up? You can double-check this by adding ip addr > ~/ip.log at the top of your script. Remember the RPi is quite slow so it might be that the DHCP havn't fully gotten an IP yet. – Torxed Jan 6 '16 at 23:55
  • I'm using root's crontab -e. I added whoami and ip addr to crontab -e, etc/rc.local, and /etc/network/if-up.d/sayhi (one after another) and they all yield root as user. The ip.log in all three cases tells me that eth0 is down and has no local IP, yet. Even the if-up.d script. I also added post-up /etc/network/if-up.d/sayhi to the eth0 interface in /etc/network/interfaces. Still, ip.log says eth0 is down. BUT: If I only restart my network connection (and not the Pi itself) with sudo /etc/init.d/networking restart, then eth0 is shown as up in ip.log – nieswand Jan 7 '16 at 13:05
  • I followed the instructions given on the page you linked to. It changes nothing on the outcome of my script. I also added a sleep time, but no matter how long I let the script sleep, the outcome is always the same. In fact, I can't log in via ssh during the sleep time. So it seems that the setup of the IP is delayed until after my script has finished. So sleeping is not a solution – nieswand Jan 8 '16 at 1:14
  • That was an error on my end. sleep now works for me. Since it is just a small feature that might be replaced with something more suitable as soon as I learn a bit more about Linux, this quick fix will do. Thanks a lot! Since I'm new to this: Should I change the question? Because the actual problem changed somewhat during the discussion. – nieswand Jan 8 '16 at 11:36

You need to tell ssh to create a terminal device on the target box, so that write will work. You do this with the -t flag. Twice.

ssh -tt 'laptop_user'@'laptop_ip' "echo '### RaspberryPi 2 online ###' | /usr/bin/write 'laptop_user' pts/0"
  • As I mentioned, write (and the whole command/script) does work. Just not when run on startup. I now believe that the reason is that the Pi does not have a local IP when the scripts in /etc/network/if-up.d are run. – nieswand Jan 7 '16 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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