Help with init script

I wrote a init script for sniproxy and enabled it.

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

START=50
STOP=50

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

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 ?

UPDATE:
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 https://github.com/dlundquist/sniproxy

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: https://github.com/openwrt/packages/blob/master/net/https-dns-proxy/files/https-dns-proxy.init

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
Summary
#!/bin/sh /etc/rc.common
# Example procd sniproxy script

USE_PROCD=1
START=50

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

stop() {
        procd_kill(instance)
        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.

Summary
~/lib/functions# cat procd.sh

# 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)
#
2 Likes

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

and

#     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

USE_PROCD=1
START=99


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


stop_service() {
    #killall sniproxy
    service_stop /mnt/data/openwrt/system-root/usr/bin/sniproxy  -a 192.168.1.1 -w 8 --socks5 192.168.1.1:9050
    #procd_kill(instance)
    logger stopped sniproxy
}
procd_kill(instance)

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:

OK!

You have a way ahead.

TLTFB later