It is currently Tue Dec 01, 2020 2:01 am
All times are UTC + 8 hours

Question: setup scheduled task

Share your awesome tips and tricks here.

Question: setup scheduled task

Postby shezhar » Tue Jun 30, 2020 7:24 pm

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

How can I do this?
shezhar
 
Posts: 4
Joined: Wed Oct 09, 2019 10:26 pm

Re: Question: setup scheduled task

Postby father.mande » Tue Jun 30, 2020 8:25 pm

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.
AS5202T /AS5002T / AS202TE / AS1002T
My Blog specific to my APKG : https://blog.father-mande.ovh/
User avatar
father.mande
 
Posts: 1047
Joined: Sat Sep 12, 2015 2:55 am

Re: Question: setup scheduled task

Postby shezhar » Tue Jun 30, 2020 9:13 pm

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
shezhar
 
Posts: 4
Joined: Wed Oct 09, 2019 10:26 pm

Re: Question: setup scheduled task

Postby Nazar78 » Wed Jul 01, 2020 2:37 am

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 - [40TB N300 RAID10 + 5 Bay USB: 8TB RAID5 & 480GB SSD for Apps]
User avatar
Nazar78
 
Posts: 201
Joined: Wed Jul 17, 2019 10:21 pm
Location: Singapore

Re: Question: setup scheduled task

Postby shezhar » Wed Jul 01, 2020 4:00 am

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.
shezhar
 
Posts: 4
Joined: Wed Oct 09, 2019 10:26 pm

Re: Question: setup scheduled task

Postby Nazar78 » Wed Jul 01, 2020 4:29 am

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 - [40TB N300 RAID10 + 5 Bay USB: 8TB RAID5 & 480GB SSD for Apps]
User avatar
Nazar78
 
Posts: 201
Joined: Wed Jul 17, 2019 10:21 pm
Location: Singapore

Re: Question: setup scheduled task

Postby father.mande » Wed Jul 01, 2020 5:05 am

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.
AS5202T /AS5002T / AS202TE / AS1002T
My Blog specific to my APKG : https://blog.father-mande.ovh/
User avatar
father.mande
 
Posts: 1047
Joined: Sat Sep 12, 2015 2:55 am

Return to Tips & Tricks

  • You cannot post new topics in this forum
    You cannot reply to topics in this forum
    You cannot edit your posts in this forum
    You cannot delete your posts in this forum
    You cannot post attachments in this forum
  • Who is online

    Users browsing this forum: No registered users and 3 guests