# Help for a script - confusing with daemon and service



## Ernie95 (Feb 5, 2022)

Hi All,

Thanks in advance for your help, as I am discovering FreeBSD.

I am trying to have AdGuardHome in a jail. Thanks to Thogs for the HowTo (https://forums.freebsd.org/threads/guide-how-to-install-adguardhome-on-freebsd.75496/).

My jail works, I have FreeBSD 12.3 and I successful installed AdGuardHome.

But I am trying to have a script which restarts AdGuardHome if it stops.

The HowTo proposes to create a service (I suppose as it) with a specific file: /etc/rc.d/adguardhome
The content is

```
#!/bin/sh
. /etc/rc.subr
name="adguardhome"
rcvar="adguardhome_enable"
adguardhome_user="root"
adguardhome_command="/opt/AdGuardHome/AdGuardHome"
pidfile="/var/run/${name}.pid"
command="/usr/sbin/daemon"
command_args="-P ${pidfile} -r -f ${adguardhome_command}"
load_rc_config $name
: ${adguardhome_enable:=no}
run_rc_command "$1"[/ICODE]

I found a script for relaunching AdGuardHome but it seems that it scans the daemon:
[CODE]#!/bin/sh
SERVICE="AdGuardHome"
if
 /usr/bin/pgrep -x "$SERVICE" >/dev/null
then
 exit 0
 #echo "$SERVICE is running"
else
nohup /AdGuardHome/AdGuardHome &
 # mail
fi
```

What I observed before to implement the script:
- AdGuardHome is running and if there is a reboot, it is launched (rc.conf contains the relevant row for this)
- if I use : service adguardhome stop, the service is stopped (based on the code of adguarhome file in /etc/rc.d)
- and after stopping adguardhome, pgrep AdGuardHome shows a PID : so the daemon is always launched

How can I stop the service and the daemon ? I thought that both were linked.

Sure if I launch again the service adguardhome just after stopping it, I see with pgrep 2 PID numbers and not one

Thanks for your help, and pedagogic explanations

BR


----------



## ondra_knezour (Feb 6, 2022)

See 6.26. Starting and Stopping Services (rc Scripts) and other resources linked in the chapter intro for overview how this all works.

I get lost somewhere between _shows a PID :_ and _pgrep 2 PID numbers_, so no direct answer here, but you definitely don't need another script to check if service is running. You should have `service adguardhome status` command already and than `service adguardhome status || service adguardhome start` (shell construct with OR -> if first command doesn't return what you want, run second one) may be enough. Except for looking why the service is dying in the first place


----------



## ondra_knezour (Feb 6, 2022)

Looking into linked how-to - rc.d scripts for locally installed software belongs to /usr/local/etc/rc.d, not /etc/rc.d and if there is port/package, which is (www/adguardhome), why bother with manual installation instead of `pkg install adguardhome`?


----------



## Erichans (Feb 6, 2022)

This may also be helpful: Practical rc.d scripting in BSD

Edit: I just noticed this is referenced in "Finally, there is an article on practical aspects of rc.d scripting" in the link ondra_knezour mentioned: 6.26. Starting and Stopping Services (rc Scripts).
*Note:* the link in the porters handbook mentioned in that section of chapter 6.26:


> Details on its usage can be found in the rc.d Handbook chapter.


does not work; the correct URL for this link is:
https://docs.freebsd.org/en/books/handbook/book/#configtuning-rcd
 (12.4. Managing Services in FreeBSD)


----------



## Ernie95 (Feb 12, 2022)

Thanks for your answers. I will investigate.
Why manual install ? Because I don't know very well freebsd. I will look freshport
Thanks again


----------



## Ernie95 (Feb 12, 2022)

Thanks. I solved it via a right installation (pkg install). It creates the script for service X stop/start/restart. And now the script (for checking that ADGH is launched or stopped) works well
Thanks again.


----------

