Help with init script

I wrote a init script for sniproxy and enabled it.

#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007


start() {
        logger started sniproxy
        /mnt/data/openwrt/system-root/usr/bin/sniproxy -a -w 8 --socks5 &

stop() {
        killall sniproxy
        logger stopped sniproxy

but when I restart router it is not running.
I see in log
Wed Jan 25 13:10:40 2023 user.notice root: started sniproxy

I think it needs to run as screen or something like that for it to stay running.
any help would be great.

try replacing START=50 with 99, then disable and enable the service,
to get new files in /etc/rc.d

if it doesn't work, try adding a sleep to the start(), it might be a simple race condition.

did you add sniproxy to openwrt by yourself, AFAIK it's not in the
official package repository ?

1 Like

I have cross-compiled it.
btw it is not those famous reverse proxy sniproxies that are for multi-domain hosting.
it is this package

also about your suggestion:
in logead I see the
Wed Jan 25 13:10:40 2023 user.notice root: started sniproxy
so I see that the script is run but maybe because it is not a daemon I should run it in screen ?

raising to 99 worked.
thank you.

should I use 99 for all my custom apps that usually only works after whole device dont its startup stuff?

Ah, I thought perhaps it was

depends what they do, but if they rely on the network, I'd use 99, to make them start last.

I copied from tor, as it uses 50.
thank you for the help.

1 Like

can you help me a bit more?

my program crashes (or exits or something) sometimes randomly.
that is not that critical of app for me to find its issue and it is abandoned.

I just care that if it is not running it should run again.

how can I do that? is it possible to do that with the init.d script?

Could run it in a loop, but I'm not sure if that's good practice for a daemon...

I think I need this but the wiki is not really good with step by step.

I only have experience with PROCD scripts, but I don't think backgrounding is needed for the init.d scripts, so I'd try removing the &.

But also do consider switching to PROCD, especially if you want to submit the PR to have the package included in the OpenWrt packages repo.

There are many examples, but if you follow https-dns-proxy init script and have any questions, give me a shout.

I think I have to go PROCD for the failover-restart.
can you put a sample for that ?
the included init.d in openwrt are too complicated for me to learn from.

The one I wrote is here:

It would have a ton of stuff you won't need for your script, there are probably smaller/easier to read PROCD scripts, maybe someone else will chime in on that.

However it's a good example if there could be multiple instances of your service and shows you how to pull variables from config file and also has an example of adding firewall objects if you need to manipulate firewall settings from the init script.

This is just a stab in the dark. Putting in a bit of time, so don't blame the messenger. ¯\_(ツ)_/¯

  • Put this in /etc/init.d/sniproxy
  • Make it executable
  • Call /etc/init.d/sniproxy start
#!/bin/sh /etc/rc.common
# Example procd sniproxy script


start_instance() {
    echo "opened sniproxy instance"
    logger opened sniproxy instance 
    procd_set_param command /mnt/data/openwrt/system-root/usr/bin/sniproxy -a -w 8 --socks5
    procd_set param respawn
    procd _close_instance

stop() {
        logger stopped sniproxy

Use htop to see if the service is running, logread -e sniproxy

Just a bit of reference going forward. This pretty well sums up the documentation.

~/lib/functions# cat

# procd API:
# procd_open_service(name, [script]):
#   Initialize a new procd command message containing a service with one or more instances
# procd_close_service()
#   Send the command message for the service
# procd_open_instance([name]):
#   Add an instance to the service described by the previous procd_open_service call
# procd_set_param(type, [value...])
#   Available types:
#     command: command line (array).
#     respawn info: array with 3 values $fail_threshold $restart_timeout $max_fail
#     env: environment variable (passed to the process)
#     data: arbitrary name/value pairs for detecting config changes (table)
#     file: configuration files (array)
#     netdev: bound network device (detects ifindex changes)
#     limits: resource limits (passed to the process)
#     user: $username to run service as
#     group: $groupname to run service as
#     pidfile: file name to write pid into
#     stdout: boolean whether to redirect commands stdout to syslog (default: 0)
#     stderr: boolean whether to redirect commands stderr to syslog (default: 0)
#     facility: syslog facility used when logging to syslog (default: daemon)
#   No space separation is done for arrays/tables - use one function argument per command line argument
# procd_close_instance():
#   Complete the instance being prepared
# procd_running(service, [instance]):
#   Checks if service/instance is currently running
# procd_kill(service, [instance]):
#   Kill a service instance (or all instances)
# procd_send_signal(service, [instance], [signal])
#   Send a signal to a service instance (or all instances)

I already have the service running.
but I saw that it was nor running after some hours, so I wanted to be sure I didnt have to manually restart the script.

I understand that.

and you said


#     respawn info: array with 3 values $fail_threshold $restart_timeout $max_fail

I understand that.
thank you for the answer.
I was hoping for someone to write me a script :wink:
I try to write one to see what happens and also try not to be a lazy boy.

did you run my example script?

it didnt work.
logread doesnt show any log.

after some trial and error, this seems to work:

#!/bin/sh /etc/rc.common
# Example procd sniproxy script


start_service() {
    logger "opened sniproxy instance"
    procd_set_param command /mnt/data/openwrt/system-root/usr/bin/sniproxy -a -w 8 --socks5
    procd_set_param respawn  ${respawn_threshold:-3600}       ${respawn_timeout:-5} ${respawn_retry:-5}

stop_service() {
    #killall sniproxy
    service_stop /mnt/data/openwrt/system-root/usr/bin/sniproxy  -a -w 8 --socks5
    logger stopped sniproxy

this gives error "
/etc/rc.common: /etc/init.d/sniproxy: line 19: syntax error: unexpected word (expecting ")")

and this didnt work:
killall sniproxy

try skipping the parameters, unless you've got several of these running ...
or try " " around the whole sniproxy command line.

I'm guessing though :wink:


You have a way ahead.

TLTFB later