# Running a zfs snapshot in rc.local 2 minutes after boot



## Alain De Vos (May 25, 2021)

It is i think something with zsh -c "mycommand"
Something with an &
Something with nohup
Something like ?

```
nohup /usr/local/bin/zsh -c "sleep 120;logger snapshot0h;nohup /root/bin/snapshot_usr_home_0h &" &
```
There is also "daemon" ???
The script produces some output.
The script has "pv" in it showing how far
The proces dies without a snapshot, I'm doing something wrong because it does not work non-interactive.
Running " /root/bin/snapshot_usr_home_0h" from command line works fine.
Do rc.local scripts have the right root privileges ?


----------



## Alain De Vos (May 26, 2021)

Without pv it worked. Why I don't know.
And how many nohup or & i need is also unknown to me.
For weekly i have,

```
export weekday=`date | awk '{print $3 %7 }' `
echo $weekday
if [[ $weekday -eq 0 ]]
then
    nohup /usr/local/bin/zsh -c  "sleep 300;sleep  3000;logger snapshotweek;nohup /root/bin/snapshot_usr_home_weekly &" &
fi
```


----------



## covacat (May 26, 2021)

rc scripts don't have /usr/local/bin in $PATH
use
weekday=$(date +%w) or date +%a

```
[09:09:54] [host!user]~$nohup sh -c "date +%s;sleep 10;date +%s" &
[1] 82209
[09:09:57] [host!user]~$appending output to nohup.out

[1]+  Done                    nohup sh -c "date +%s;sleep 10;date +%s"
[09:10:20] [host!user]~$cat nohup.out
1622009397
1622009407
```


----------



## tux2bsd (May 26, 2021)

cron is literally for this sort of thing, for your first question look at @reboot

```
cat /etc/crontab
@reboot root  sleep 120; /usr/local/bin/example_script
```
note, use lock files to prevent overlapped running in your script

or you can do it in root's crontab with `crontab -e` (note the user is not specified below, but is in /etc/crontab (see above))

```
# crontab -l
#after boot
@reboot sleep 120; /usr/local/bin/example_script
#weekdays, 1 minute past midnight.
1 0 * * 1-5 /usr/local/bin/example_script
```


----------



## mtu (May 26, 2021)

I call a script with a `sleep` command from /etc/rc.local with `&`:

```
# cat /etc/rc.local
/root/hdd/sleeptimer.sh &
```
The script contains:

```
#!/bin/sh
sleep 120
camcontrol standby ada1 -t 1100
sleep 2
camcontrol standby ada0 -t 1110
[…]
```


----------



## Alain De Vos (May 26, 2021)

covacat said:


> rc scripts don't have /usr/local/bin in $PATH
> use
> weekday=$(date +%w) or date +%a
> 
> ...


changed it to, including full path,

```
export weekday=`/bin/date | /usr/bin/awk '{print $3 %7 }' `
echo $weekday
if [[ $weekday -eq 0 ]]
then
/usr/local/bin/zsh -c      "sleep 300;sleep  3000;logger snapshotweek;/root/bin/snapshot_usr_home_weekly" &
fi
```


----------



## Alain De Vos (May 26, 2021)

I had to add nohup. Nohup and & did the job.


----------

