Running program as service in background

  • ATTENTION! You'll notice a Prefix dropdown when you create a thread. If your post applies to one of the topics listed, please use that Prefix for your post. When browsing the thread list you can use the Prefix to filter the view.
  • ATTENTION! As of November 1, 2020, you are not able to reply to threads 6 months after the thread is opened if there are more than 500 posts in the thread.
    Threads will not be locked, so posts may still be edited by their authors.
    Just start a new thread on the topic to post if you get an error message when trying to reply to a thread.

SamHNB

Occasional Visitor
I have a web-based Wake-On-Lan (WOL) utility build using Go language. This can wakeup the computer directly over HTTP using the computer name in URL. This is very helpful for me to have this included in my DevOps automation when I can wake the machine without any user intervention. I took the source from Github modified it little and compiled it to run on Asuswrt-Merlin running on RT-AC68U. I am using this utility behind NGINX.

Everything works except - I could not figure out how to make this utility work as daemon (service) which starts automatically after boot.

I was able to manually make it run in background with the command:

Code:
/mnt/entware/WolWeb/wolweb > /mnt/entware/WolWeb/wolweb.log 2>&1 &
I tried to put the same command in /jffs/scripts/services-start but it does not run. I am not even sure if that script is called by entware.

Can we make this into service which can be start/stop/restart from command line? I don't think we can use unit file to make this as service.

I would appreciate any guidance.
 

ColinTaylor

Part of the Furniture
If your program was compiled with dependencies on Entware then you would have to start it via an Entware S* script in /opt/etc/init.d (remembering to include all the necessary PATHs in the script).
 

SamHNB

Occasional Visitor
If your program was compiled with dependencies on Entware then you would have to start it via an Entware S* script in /opt/etc/init.d (remembering to include all the necessary PATHs in the script).
Thank you. Step in right direction, I guess.

I created a script and named it /opt/etc/init.d/S90wolweb and it was called during the entware initialization.

However, since the program does not follow the standard daemon construct, I guess it leaves the script running waiting for the program to terminate.

2020-10-31 19_18_14-Clipboard.png


I don't think it is perfect and not sure what other consequences it might have. Other scripts in init.d with S* do finish after starting the programs, that I think is because the other programs are built as daemon.
 
Last edited:

ColinTaylor

Part of the Furniture
Other scripts in init.d with S* do finish after starting the programs, that I think is because the other programs are built as daemon.
Indeed. I would assume that you would have to run the program as a background process (&) and possibly with nohup as well. But it probably really depends on whether your program was designed to run continuously in background.
 
Last edited:

SamHNB

Occasional Visitor
Indeed. I would assume that you would have to run the program as a background process (&) and possibly with nohup as well. But it probably really depends on whether your program was designed to run continuously in background.
Ok, after trials and errors I have now managed to run the program as service in background. This is my /opt/etc/init.d/S90wolweb file.

Bash:
#!/bin/sh

PATH=/tmp/mnt/entware/WolWeb:/opt/sbin:/opt/bin:/opt/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

case $1 in
    start)
    logger -t wolweb "wolweb starting..."
    wolweb > /tmp/mnt/entware/WolWeb/wolweb.log 2>&1 &
    logger -t wolweb "wolweb started..."
    ;;
    stop)
    logger -t wolweb "wolweb stopping..."
    pkill wolweb && echo 'wolweb force stopped.'
    logger -t wolweb "wolweb stopped..."
    ;;
    restart)
    logger -t wolweb "wolweb stopping..."
    pkill wolweb && echo 'wolweb force stopped.'
    logger -t wolweb "wolweb stopped..."
    logger -t wolweb "wolweb starting..."
    wolweb > /tmp/mnt/entware/WolWeb/wolweb.log 2>&1 &
    logger -t wolweb "wolweb started..."
    ;;
esac
This is how the process look now in htop

2020-11-01 11_42_50-10.10.10.1 - PuTTY.png
 

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top