Running script on startup, Fedora 30

I have a script that runs forever using watch. It is started with watch -n1 -x /bin/bash /sh/lapse.sh &>/dev/null &, contained in the file /root/startsnap.sh. The script contains

ISMOUNTED=`mount | grep -c unit_1`
if [ $ISMOUNTED -eq 1 ]; then
        FILENAME=/share/unit_1/public/henyard/`date +%Y%m%d%H%M%S`.jpg
        wget -qO $FILENAME http://10.168.0.200/ISAPI/Streaming/channels/1/picture && chmod 664 $FILENAME && chown username.groupname $FILENAME
fi

When I start the script manually, it runs perfectly. However, I would like it to start at startup. To that end, I have @reboot /bin/bash /root/startsnap.sh in crontab.

The script does not run after reboot. journalctl does not show anything related to this. Setting up a systemd service did not work, either. Nothing seems to run.

How do I run this script at startup?

Hi,

it may throwing an error, because the shebang is missing from the begining of the script, please try making this the first line:

#!/bin/bash

You could also temporarily direct standard output & error to a log file for the cronjob, it may give insight to whats going on; try:

@reboot /bin/bash /root/startsnap.sh &>> /root/startsnap.log

Thanks Tom.

There are several ways you can use them, including creating systemd unit .

https://www.linode.com/docs/quick-answers/linux/start-service-at-boot/

https://unix.stackexchange.com/questions/47695/how-to-write-startup-script-for-systemd

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-unit_files

https://medium.com/@benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6

And you can try make desktop entry file and put it in /etc/xdg/autostart or for user wide ~/.config/autostart

file example :
https://github.com/yucefsourani/arpytrans/blob/master/arpytrans.desktop

1 Like

Possibly the easiest way would be to put it in /etc/rc/rc.d and name it rc.local. This way it will get executed at the very end of startup by rc.local.service, which is automatically acitve if that file exists. One nice thing about Linux is that there’s almost always more than one way to do just about anything, so that if you’re having trouble getting one of them to work, there are others you can try.

Thank you for this suggestion. As per the original post, the systemd service refuses to do anything. And I did not mention in the post that I do not log onto that computer locally and never start the graphical user interface. I will, however, read the other links you mentioned regarding systemd.

Thanks. I was thinking myself of the rc.local first but it has been my understanding that all the easy, guaranteed-to-work features of Fedora are being phased out and replaced by unnecessarily complex and bloated options with either too sparse or excessively detailed help or man pages (replace /var/log/messages with journalctl, replace /etc/init.d with systemd, replace network scripts by NetworkManager etc.) and therefore didn’t honestly even expect it work anymore.

I will give thiis a try.

Edit: the log file says: Error opening terminal: unknown.

Ideas?

Hi,

Not sure what the error means, but I suspect cron is starting the script before the network is up, which maybe causing an issue. My only other suggestion is systemd:

[Unit]
Description=Start Snap
Wants=network-online.target
After=network.target network-online.target

[Service]
Type=oneshot
ExecStart=/root/startsnap.sh

[Install]
WantedBy=basic.target

Thanks Tom.

systemd-run /path/to/script.sh
journalctl -u unit_name

What does it say?

Got it fixed based on the error message. At that stage, the variable TERM is not yet set, and therefore watch does not know where to put its content. I added export TERM=linux to the script and now it works.

2 Likes

Although watch is probably not a command you should use in a non-interactive script.

I will happily use some other method when one comes up with a way of running a command every 1 or 2 seconds until terminated. Crontab does not work for this purpose.

while sleep 2
do
    ...
done

I’m sure I had some reason why I didn’t choose this. Maybe I tried it first and noticed it wouldn’t do what I wanted (probably because of the missing TERM variable) and chose another way. Maybe I wanted something that would spawn a background process and settled for watch. Who knows, I cannot remember.

I do thank you for that reply, as well, and am happy with my watch solution.