2

I have an IoT device with a barebones Linux distro on it. No crontab, chkconfig, or update-rc.

How would I go about running a script when the device has started, after everything on the device has been loaded? I thought I could just add the script to /etc/init.d but nothing seems to happen. This is the script I'm using.

#! /bin/sh -e
# /etc/init.d/camstart

. /etc/init.d/functions.sh

start() {
    /usr/local/packages/application/myapp
}

stop() {
    kill `pidof myapp`
    kill `pidof myappinternal`
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        pidof myapp
        pidof myappinternal
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0

My other idea was to use /etc/rc.local, but the device does not have that file. It has rc?.d folders ranging from 1-6 and S. Placing scripts in any of them doesn't seem to have any affect.

Any suggestions?

EDIT: I'm not sure what Linux distro it is, but checking /etc/*release tells me that it is based on Poky from the Yocto Project. From /proc/version it's Linux version 4.9.62.

As far as I can tell, it uses systemctl to start systemd services.

EDIT2: Forgot to mention. I've tried writing a systemd service for my script and starting it using systemctl start app but it exits immediately with exit code 0.

app.server:

[Unit]
Description=app
After=acap-pre.target
Requires=acap-pre.target

[Service]
Slice=extension-acap.slice
Type=oneshot
RemainAfterExit=yes
ExecStart=/etc/init.d/app start
ExecStop=/etc/init.d/app stop

FINAL EDIT: Got it to work after looking at How to write startup script for systemd and saw that my systemd service was missing

[Install]
WantedBy=multi-user.target
  • 3
    As you can see, answerers are going to adopt your unwarranted assumption that you even have van Smoorenburg init+rc on that system. Find out what softwares actually do perform startup and shutdown, which could be anything from BusyBox init to runit, and tell answerers that in the question. Or tell answerers what the operating system actually is, rather than a vague and unhelpful "Linux distribution", and thereby enable them to try to find out for themselves. – JdeBP Jun 20 '18 at 13:42
  • Do you have a desktop on your system or is it headless ? – Kiwy Jun 20 '18 at 14:14
  • Headless. Only have a terminal via ssh. – Kagemand Andersen Jun 20 '18 at 14:16
  • did you read this question/answer https://unix.stackexchange.com/q/47695/53092 – Kiwy Jun 20 '18 at 14:29

1 Answers1

1

The procedure to start automatically a daemon in runlevel 3 is easy:

  1. place your script in /etc/init.d,
  2. check permissions and ownership of the script,
  3. create a symbolic link in /etc/rc3.d to the script with the following name S99scriptmame.

The S means start the service. The number is used to sort the service start order from 01 (high priority script) to 99 (last priority script).

Probably, do you need to create a link to the script in /etc/rc0.d (halt) and /etc/rc6.d (reboot) named K01scriptname. Usually last started deamon is the first to be stopped.

See man init.d^1 for a complete explanation.

andcoz
  • 17,130