0

I am trying to run a script on reboot that enables a port forward to a remote server. I made i script that it works if i run it in terminal. But i am not able to run it on crontab reboot:

@reboot sleep 120000 && sh /home/user/doit.sh

where i can see a errlog for the crontab? I have another script in crontab it reboot command and it works. This one no. This script calls SSH to make a tunnel. Thank you.

Kaf
  • 169
  • 8
  • 1
    Please add some information on your OS. Without it it is just guesswork. `sleep`'s argument is in seconds, so you specified a one day and a half gap between reboot and the call to your script. On the other hand, there are better ways to run a command when the network goes up. What to use may depend on the Linux distribution. – Piotr P. Karwasz Dec 05 '19 at 16:30
  • i thought the sleep was in miliseconds. Changed it... My OS is Ubuntu Server 16.04 – Kaf Dec 05 '19 at 17:46
  • Fixed it!. There was one charater wrong and the sleep was in miliseconds. Thank you. – Kaf Dec 05 '19 at 17:50

1 Answers1

1

Since you have systemd-based system, you might consider adding a .service file, e.g. /etc/systemd/system/ssh-tunnel.service with the following content:

[Unit]
Description=SSH tunnel
After=network.target

[Service]
Type=forking
ExecStart=/home/user/doit.sh

[Install]
WantedBy=multi-user.target

You might change the unit type to Type=simple if your script does not fork. Afterwards you just have to reload systemd, enable the unit and try starting it:

systemctl daemon-reload
systemctl enable ssh-tunnel.service
systemctl start ssh-tunnel.service

If everything works fine, the tunnel will be established as soon as networking is ready.

Piotr P. Karwasz
  • 5,292
  • 2
  • 9
  • 20
  • does not work: Failed at step EXEC spawning /home/user/doit.sh: Exec format error. I have done chmod +x /home/user/doit.sh – Kaf Dec 05 '19 at 19:37
  • besides the chmod +x i need to add a shebag at the script:#!/bin/bash and this worked. Thank you – Kaf Dec 05 '19 at 19:43