1

I'm trying to get a script to run at startup, but does nothing if I've connected to my Raspberry Pi via SSH.

So far I've got the crontab to automatically run the script checkssh.sh via @reboot sleep 30 && sudo bash ./checkssh.sh and './checkssh.sh' contains this:

#!/bin/bash

if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
  echo "SSH CONNECTED"
else
    ./autobackup.sh
fi

Running checkssh.sh from an SSH terminal returns 'SSH CONNECTED' which is expected, and letting it run automatically from the crontab at reboot when SSH isn't connected works correctly. However, when it runs at boot and I connect via SSH as soon as it's available, it still runs the script. I'm not sure where this is going wrong.

I need it to run automatically and if there's no SSH connection run autobackup.sh , but if there is an SSH connection, not to run anything. The device I use for the SSH connection may vary & the network used may also vary, so a script that relies on specific IP's isn't ideal.

Thanks for any help :)

1
  • cron and sshd are two very separate programs, and neither is relevant to running services at start-up. I don't know what ships with a Raspberry Pi, but you are looking at properly configure something like upstart or systemd to run autobackup.sh on startup. Commented Jun 11, 2017 at 15:17

2 Answers 2

1

Those environment variables (SSH_CLIENT and SSH_TTY) are only set in the environment of an SSH session. You cannot check them from another process and expect them to fulfill your goals here.

Instead, run the program finger. This is the standard way to see who is logged in.

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

1 Comment

Not really sure how to go about using finger. If the SSH_CLINET & SSH_TTY are only set once SSH is connected, could I have this script run at login +30s instead, and make the pi login automatically?
0

Probably you need to add some delay before running your script to allow for the SSH service to come up. If cron service comes up before the sshd does, you will have a failure. Try:

@reboot sleep 60 && bash ./checkssh.sh

Also I would substitute the '.' with the full script path.
In one scenario I had to add as many as 120 seconds to get the @reboot crontab to work right. But ssh should not need as much. I guess you can trim 60 seconds according to your needs after you get it working.

1 Comment

I've already got a sleep on the cronjob, and have tried different timings. Within autobackup.ssh there's a confirm dialogue that if I don't respond within 60s, it timesout and runs the rest of the script, or I can cancel it. The trouble is that this dialogue doesn't get outputted to the SSH terminal, so I have no way of responding as is. I have managed to get round this by using screen so that the script is ran in a new screen which when I ssh in, I can do screen -r and see the dialogue there. Seems a bit messy though, but screen will let me exit ssh while the script is running at least

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.