Question: setup scheduled task

Share your awesome tips and tricks here.

Moderator: Lillian.W@AST

Post Reply
shezhar
Posts: 4
youtube meble na wymiar Warszawa
Joined: Wed Oct 09, 2019 10:26 pm

Question: setup scheduled task

Post by shezhar »

I would like to setup an scheduled task to deactivate and reactivete the surveillance center app every 2 days.

How can I do this?
User avatar
father.mande
Posts: 1815
Joined: Sat Sep 12, 2015 2:55 am
Location: La Rochelle (France)

Re: Question: setup scheduled task

Post by father.mande »

Hi,
shezhar wrote:I would like to setup an scheduled task to deactivate and reactivate the surveillance center app every 2 days.
How can I do this?
Don't know if it's include in the application itself ... if not use cron (shell started based on time schedule)
... cron run in background and start shell at some time / delay / days / etc.
... the shell can do a restart / start / stop of the application OR use apkg program to enable / disable "surveillance center" so are not dependent of any reboot or crash ... stop if not disable ... do a start at next boot ... disable do stop and disabled ... so don't start again up to next enable.

cron is documented in Asustor web site.

Philippe.
AS6602T / AS5202T /AS5002T / AS1002T / FS6706T
shezhar
Posts: 4
Joined: Wed Oct 09, 2019 10:26 pm

Re: Question: setup scheduled task

Post by shezhar »

Hi, thanks for your answer.

as i am new to linux, i realy just understand the half of your text.

i have heard about cron. i am able to use ssh.

but i struggle to find and use the right script, or know/setup the right prerequisites.
so maybe you could text an script example (entware installed).

here is my idea:

#!/bin/sh
apkg --disable "nvr"
sleep 20
apkg --enable "nvr"

i also did not find the cron documentation on asustor website, just the normal ubuntu cron wiki
User avatar
Nazar78
Posts: 2059
Joined: Wed Jul 17, 2019 10:21 pm
Location: Singapore
Contact:

Re: Question: setup scheduled task

Post by Nazar78 »

You don't need a script for this, just run "crontab -e" while login as root and add below to the last line in the vi editor (vi basics, "i" to insert, ESC+:wq to save and quit, ESC+:q! force quit w/o saving, you can google more).

Code: Select all

# Every 1AM restart Surveillance Center if enabled
0 1 * * * apkg --info-installed nvr|grep -q 'Enabled: Yes' && { apkg --disable nvr && sleep 20 && apkg --enable nvr; }
If you're still new to this, best practice is to backup the crontab as some jobs could fail if the cron is messed up.

Backup to old and new to edit:

Code: Select all

crontab -l|tee asustor-old.cron asustor-new.cron
Edit the new cron:

Code: Select all

vi asustor-new.cron
Import back to crontab:

Code: Select all

crontab < asustor-new.cron
Edited: I recommend removing the 20 secs of sleep, if the system restart or crash, the app will be left unknowingly disabled. Without sleep it will restart just fine or safer bet is don't use apkg but call the start-stop script instead e.g.:

Code: Select all

# Every 1AM restart Surveillance Center if enabled
0 1 * * * apkg --info-installed nvr|grep -q 'Enabled: Yes' && /volume1/.@plugins/AppCentral/nvr/CONTROL/start-stop.sh restart
AS5304T - 16GB DDR4 - ADM-OS modded on 2GB RAM
Internal:
- 4x10TB Toshiba RAID10 Ext4-Journal=Off
External 5 Bay USB3:
- 4x2TB Seagate modded RAID0 Btrfs-Compression
- 480GB Intel SSD for modded dm-cache (initramfs auto update patch) and Apps

When posting, consider checking the box "Notify me when a reply is posted" to get faster response
shezhar
Posts: 4
Joined: Wed Oct 09, 2019 10:26 pm

Re: Question: setup scheduled task

Post by shezhar »

thanks. this is very usefull and much simpler. I try the command, nvr get in disable state but isnt restarting. Maybe something is wrong? EDIT: Second Command is working very well

in the meantime i build up a working shell script solution. I am not new to scripting, but new to unix
As i assume not everyone knows the right commands and how it works, i will describe my way:

Prerequisites:
know how to use ssh e.g. with putty
inform about basic ssh commands
know how to use vi editor
inform about crontab syntax (timer command)
(as non unix user its bothering me, that you have to code everything, no gui) :roll:

first step: create script file

Code: Select all

touch /usr/var/restartnvr.sh
Hope this direction wont be updated, and the file deleted

second step: edit script file

Code: Select all

vi /usr/var/restartnvr.sh
and enter
#!/bin/sh

apkg --disable nvr
sleep 20
apkg --enable nvr
save and exit (ESC+ ":wq")

third step: create cronjob (i have to use sudo command, you should try without)

Code: Select all

sudo crontab -e
enter below last line (type i for insert mode)
0 2 */2 * * /usr/bin/sh /usr/var/restartnvr.sh
This will start the script file at 2am every 2 days.

I hope this will be usefull for someone with less expirience.
User avatar
Nazar78
Posts: 2059
Joined: Wed Jul 17, 2019 10:21 pm
Location: Singapore
Contact:

Re: Question: setup scheduled task

Post by Nazar78 »

I don't know why it never restarted, perhaps there's no exit 0 returned, if that's the case change "&&" to ";". You can play around though. I don't use this app but using my own script instead calling ffmpeg to record few months of rtsp footage.
Hope this direction wont be updated, and the file deleted
PS: Don't use /usr/var as the rootfs mount will revert at boot, so use /root or other location instead.
AS5304T - 16GB DDR4 - ADM-OS modded on 2GB RAM
Internal:
- 4x10TB Toshiba RAID10 Ext4-Journal=Off
External 5 Bay USB3:
- 4x2TB Seagate modded RAID0 Btrfs-Compression
- 480GB Intel SSD for modded dm-cache (initramfs auto update patch) and Apps

When posting, consider checking the box "Notify me when a reply is posted" to get faster response
User avatar
father.mande
Posts: 1815
Joined: Sat Sep 12, 2015 2:55 am
Location: La Rochelle (France)

Re: Question: setup scheduled task

Post by father.mande »

Hi,

In complement
... use always full path-name for all executable ex. not apkg but /usr/sbin/apkg ... or set the PATH variable (cron is started at init phase ... so without full PATH)
... take care if you use cron from Entware
... ... because two cron exist one crond from Asustor one cron from Entware, verify which is in used (which command) or again use full path-name
... ... ex. /usr/bin/crontab is for Asustor ... when /opt/bin/crontab is for changing cron in Entware
... ... no problem for manager one is crond in Asustor when it's cron in Entware

Last to restart (and validate cron) before a reboot do :
/etc/init.d/S41crond stop then /etc/init.d/S41crond start ... for Asustor (no restart)
or
/opt/etc/init.d/S10cron restart ... for Entware cron
... other options exist : Usage: /opt/etc/init.d/S10cron (start|stop|restart|check|kill|reconfigure)

Entware cron is automatically started at boot when Entware is Enable. (cron from Entware support more options ... it's not applet of busibox but full GNU cron)

Philippe.
AS6602T / AS5202T /AS5002T / AS1002T / FS6706T
Post Reply

Return to “Tips & Tricks”