Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

synology NAS: (date + time) check of the file /var/log/messages - don't know exactly how...

419 views
Skip to first unread message

someone

unread,
Apr 7, 2013, 8:24:03 PM4/7/13
to
Hi,

I have a NAS (synology) and I use a program called "Advanced Power Manager" to schedule the time for automatic power on. I want to turn on the NAS at e.g. 0200 hours mon-fri and then it should run a script (let's call it MY_DAILY_JOB.sh) and then there are two options:

1) If the NAS was already on, then DON'T turn off after finishing the MY_DAILY_JOB.sh-script
2) If the NAS was turned on (already off), then turn off the NAS after finishing the MY_DAILY_JOB.sh-script

I asked in the synology forum, about how to accomplish this task but nobody answered. Now I think I know what to do, although this is probably very ugly and probably a better solution exists. But my idea is to parse /var/log/messages from behind - it'll look something like this:

------------
Apr 8 01:59:01 kernel: [ 1421.420000] correction with 0x0f
Apr 8 01:59:01 syslogd exiting
Apr 8 02:03:08 syslogd started: BusyBox v1.16.1
Apr 8 02:03:08 kernel: [ 0.000000] CPU: Feroceon 88FR131 [56251311] revision
Apr 8 02:03:08 kernel: [ 0.000000] CPU: VIVT data cache, VIVT instruction ca
Apr 8 02:03:08 kernel: [ 0.000000] Machine: Synology 6282 board
Apr 8 02:03:08 kernel: [ 0.000000] Using UBoot passing parameters structure
Apr 8 02:03:08 kernel: [ 0.000000] Sys Clk = 200000000, Tclk = 166666667
Apr 8 02:03:08 kernel: [ 0.000000] Synology Board ID: 26
Apr 8 02:03:08 kernel: [ 0.000000] Memory policy: ECC disabled, Data cache w
Apr 8 02:03:08 kernel: [ 0.000000] Built 1 zonelists in Zone order, mobility
Apr 8 02:03:08 kernel: [ 0.000000] Synology Hardware Version: DS413jv10-j
Apr 8 02:03:08 kernel: [ 0.000000] Internal HD num: 4
Apr 8 02:03:08 kernel: [ 0.000000] Internal netif num: 1
Apr 8 02:03:08 kernel: [ 0.000000] Flash Memory Size: 8 MB
Apr 8 02:03:08 kernel: [ 0.000000] Console: colour dummy device 80x30
Apr 8 02:03:08 kernel: [ 0.240000] Mount-cache hash table entries: 512
Apr 8 02:03:08 kernel: [ 0.410000]
Apr 8 02:03:08 kernel: [ 0.410000] CPU Interface
.......
.......
Apr 8 02:03:27 hotplugd: DEVICE:/proc/bus/usb/001/002
Apr 8 02:03:27 hotplugd: DEVPATH:1-1:1.0
Apr 8 02:03:27 hotplugd: SUBSYSTEM:usb
Apr 8 02:03:27 hotplugd: INTERFACE:9/0/0
Apr 8 02:03:27 hotplugd: hotplugd.c:912 ##### complete device hotplug!!
Apr 8 02:03:33 cupsd[4374]: [conf.c:1893] Filter "pstoraster" not found.
Apr 8 02:03:36 ddnsd: IsDDNSEnable(ddnsd.c:2887): All Config are Disable, Stop
Apr 8 02:03:38 kernel: [ 81.500000] NFSD: Using /var/lib/nfs/v4recovery as th
Apr 8 02:03:38 sm-notify[4714]: Failed to open /var/lib/nfs/sm.bak: No such fil
Apr 8 02:03:47 synocheckhotspare: synocheckhotspare.c:129 [INFO] No hotspare co
Apr 8 02:03:49 logout.cgi: login.c (936) SynoCgiLoginRemoveUser failed.
Apr 8 02:03:58 root: /usr/local/etc/rc.d/optware.sh start executing.
Apr 8 02:03:58 root: Starting Optware.
Apr 8 02:03:59 root: /opt/etc/rc.optware start executing.
Apr 8 02:03:59 root: Beginning start of /opt/etc/init.d/S40sshd.
Apr 8 02:03:59 root: start of /opt/etc/init.d/S40sshd complete.
Apr 8 02:03:59 root: /opt/etc/rc.optware start complete.
Apr 8 02:03:59 root: /usr/local/etc/rc.d/optware.sh start complete.
Apr 8 02:03:59 root: Optware start complete.
Apr 8 02:05:18 scemd: scemd.c:420 stop manutild.
------------

Here's my idea (let me know what you think of it):

A) I'll use the "Advanced Task Manager" to turn on the NAS at given time(s)
B) I make a cronjob which runs the MY_DAILY_JOB.sh 5 minutes after the time I chose above under (A)
C) In the end of the MY_DAILY_JOB.sh I must parse /var/log/messages and maybe search for the line: "root: Optware start complete" from behind of the file.
D) I must make a script that tests if the last date (Apr 8) of /var/log/messages is today.
E) IF the date is today, then I must check the time and convert it to integer or decimal or something.
F) Then I also convert the power-on time (0200 hours) to integer and subtract the two numbers.
G) *IF* this value is below plus/minus something e.g. the NAS was turned on around 0200 hours, then it means that the NAS should also shutdown now and call my shutdown script. *IF* the NAS was already turned on, it means it's running a longer job and then MY_DAILY_JOB.sh should NOT turn off the NAS - I'll manually turn off the NAS, in this case...

Do you think this is a good way of solving the problem? It's the best, I can come up with...

I have problems doing the date and time check of /var/log/messages...

I hope someone can help me how to check these fields and/or help me a bit here...

Thanks (I'll be very grateful if someone will help me with suggestions/ideas here)!

Thomas 'PointedEars' Lahn

unread,
Apr 8, 2013, 6:44:52 AM4/8/13
to
someone wrote:

> I have a NAS (synology) and I use a program called "Advanced Power
> Manager" to schedule the time for automatic power on. I want to turn on
> the NAS at e.g. 0200 hours mon-fri and then it should run a script (let's
> call it MY_DAILY_JOB.sh) and then there are two options:
>
> 1) If the NAS was already on, then DON'T turn off after finishing the
> MY_DAILY_JOB.sh-script 2) If the NAS was turned on (already off), then
> turn off the NAS after finishing the MY_DAILY_JOB.sh-script

I do not understand why, as “Advanced Power Manager” is scheduled to turn on
your NAS at 0200, and the script always runs after that, you would still
need to test in your script whether the NAS was on. Should it not be a
given that it is turned on then?

> I asked in the synology forum, about how to accomplish this task but
> nobody answered. Now I think I know what to do, although this is probably
> very ugly and probably a better solution exists. But my idea is to parse
> /var/log/messages from behind […]

That is a Really Bad Idea. Instead, you should find a way to determine
directly the power state of the NAS when the script runs, if at all
necessary. Since “Advanced Power Manager” can power on the NAS, it may
provide an interface showing the NAS's power state. Or, as the NAS's
filesystems would need to be mounted to be used, you could check for that.

Your lines are way too long; they should not exceed the 80th plain-text
column; a maximum of 72 to 78 characters per line is customary (for proper
quoting). Please do not use Google Groups for posting to Usenet (until you
learned to work around its many flaws). And you should post using your real
name.

--
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Icarus Sparry

unread,
Apr 8, 2013, 12:41:48 PM4/8/13
to
On Sun, 07 Apr 2013 17:24:03 -0700, someone wrote:

> Hi,
>
> I have a NAS (synology) and I use a program called "Advanced Power
> Manager" to schedule the time for automatic power on. I want to turn on
> the NAS at e.g. 0200 hours mon-fri and then it should run a script
> (let's call it MY_DAILY_JOB.sh) and then there are two options:
>
> 1) If the NAS was already on, then DON'T turn off after finishing the
> MY_DAILY_JOB.sh-script 2) If the NAS was turned on (already off), then
> turn off the NAS after finishing the MY_DAILY_JOB.sh-script
>
> I asked in the synology forum, about how to accomplish this task but
> nobody answered. Now I think I know what to do, although this is
> probably very ugly and probably a better solution exists. But my idea is
> to parse /var/log/messages from behind - it'll look something like this:

You seem to be saying that the script is run on the NAS box directly, and
from the syslog output it looks like the NAS box is running some variant
of Unix.

If you have a /proc/uptime, I would use that. It has two numbers, the
first of which is the time the system has been up.

If you don't have /proc/uptime, then you might have an "uptime" command.
This is a bit trickier to parse. I would delete everything after the
first comma and before the word 'up'

P=$(uptime | sed 's/.* up \([^,]*\),.*/\1/')

then see if it matches 'min', e.g.

case $P in
([0-9]\ min) # system up for less than 10 minutes, so shut it down
shutdown -h now
;;
esac



someone

unread,
Apr 8, 2013, 1:28:31 PM4/8/13
to Thomas 'PointedEars' Lahn
On Monday, April 8, 2013 12:44:52 PM UTC+2, Thomas 'PointedEars' Lahn wrote:
>
> I do not understand why, as “Advanced Power Manager” is scheduled to turn on
> your NAS at 0200, and the script always runs after that, you would still
> need to test in your script whether the NAS was on. Should it not be a
> given that it is turned on then?

I think what I didn't explain very well is confusing you: Sometimes I use the NAS as a media center, e.g. if I watch a movie. I could also use it for sharing files on my network (typically). It could also be that I've enabled the "Download manager" and then it's just downloading big files and/or running another long/big job in addition to the 0200-hours-script.

This means: Sometimes the NAS IS already awaken when the 0200-hours script is run. *IF* this is the case, it means that maybe I'm watching a movie or otherwise using the network and then the script should *NOT* turn off the NAS again (in the middle of a movie or in the middle of downloading something or otherwise doing a big/long job). I'll manually turn it off, in this case... This raises the question about how to check if the NAS was already turned on a long ago before running the 0200-hours job or if the NAS was turned on, only for running this script (in which case, it should turn off again nicely).

I hope this explanation helped...

> > I asked in the synology forum, about how to accomplish this task but
> > nobody answered. Now I think I know what to do, although this is probably
> > very ugly and probably a better solution exists. But my idea is to parse
> > /var/log/messages from behind […]
>
> Your lines are way too long; they should not exceed the 80th plain-text
> column; a maximum of 72 to 78 characters per line is customary (for proper
> quoting). Please do not use Google Groups for posting to Usenet (until you

I didn't know quoting was a problem with google groups.

> learned to work around its many flaws). And you should post using your real
> name.

I'll google for how to fix google groups flaws in a minute. AFAIK, it doesn't matter which nickname people use. Just focus on the contents. I never heard of this "real name"-problem before. I hope focusing on shell scripting is the primary interest of people in here. Thanks.

someone

unread,
Apr 8, 2013, 1:34:11 PM4/8/13
to
On Monday, April 8, 2013 6:41:48 PM UTC+2, Icarus Sparry wrote:
> On Sun, 07 Apr 2013 17:24:03 -0700, someone wrote:
>
>
> You seem to be saying that the script is run on the NAS box directly, and
> from the syslog output it looks like the NAS box is running some variant
> of Unix.

Exactly - some kind of modified linux, I think.

> If you have a /proc/uptime, I would use that. It has two numbers, the
> first of which is the time the system has been up.

THANKS! Very good idea - MUCH better than parsing log files... I'm very grateful. Now I just need to google for how to do some mathematics on this value, I would guess other people did this before me and that google can help me find a solution.

> If you don't have /proc/uptime, then you might have an "uptime" command.

I also got that, THANK YOU!

> This is a bit trickier to parse. I would delete everything after the
> first comma and before the word 'up'

I see what you mean. Thanks!

> P=$(uptime | sed 's/.* up \([^,]*\),.*/\1/')
>
>
>
> then see if it matches 'min', e.g.
>
>
>
> case $P in
>
> ([0-9]\ min) # system up for less than 10 minutes, so shut it down
>
> shutdown -h now
>
> ;;
>
> esac

I think this could be a solution... I'm very grateful, thank you very much - I feared parsing log files would be a very cumbersome task for me to accomplish but both your suggestions should be relatively easy to implement from here, I think.

Thanks again for your help!

Thomas 'PointedEars' Lahn

unread,
Apr 8, 2013, 1:40:16 PM4/8/13
to
someone wrote:

> I'll google for how to fix google groups flaws in a minute.

No, you will not or you would have done it *before* you posted an thereby
increased your reader's risk of getting eye cancer. Again.

> AFAIK, it doesn't matter which nickname people use.

It matters to me that they do not use only a nickname. Because wish
communicate with people, not nicknames or addresses. People who would stand
by their statement with their real name (and might have a nickname on top of
that).

> Just focus on the contents.

Just go away. We really don't another luser with hypersensitive soul and
delusions of entitlement.

> I never heard of this "real name"-problem before.

Because you have no clue where you are posting to:

> I hope focusing on shell scripting is the primary interest of people in
> here.

QED.

<http://www.catb.org/~esr/faqs/smart-questions.html>

Jerry Peters

unread,
Apr 8, 2013, 4:16:38 PM4/8/13
to
someone <news...@gmail.com> wrote:
> On Monday, April 8, 2013 6:41:48 PM UTC+2, Icarus Sparry wrote:
>> On Sun, 07 Apr 2013 17:24:03 -0700, someone wrote:
>>
>>
>> You seem to be saying that the script is run on the NAS box directly, and
>> from the syslog output it looks like the NAS box is running some variant
>> of Unix.
>
> Exactly - some kind of modified linux, I think.
>
>> If you have a /proc/uptime, I would use that. It has two numbers, the
>> first of which is the time the system has been up.
>
> THANKS! Very good idea - MUCH better than parsing log files... I'm very grateful. Now I just need to google for how to do some mathematics on this value, I would guess other people did this before me and that google can help me find a solution.

GNU date can do all sorts of varied date/time calculations, see the -d
option. For example, if $upsecs is the uptime in seconds "date '+%s'
-d "now - $upsecs secs"" will calculate the start time in UTC
seconds.

Jerry

someone

unread,
Apr 8, 2013, 4:48:04 PM4/8/13
to Thomas 'PointedEars' Lahn
On Monday, April 8, 2013 7:40:16 PM UTC+2, Thomas 'PointedEars' Lahn wrote:

someone

unread,
Apr 8, 2013, 4:49:11 PM4/8/13
to
On Monday, April 8, 2013 10:16:38 PM UTC+2, Jerry Peters wrote:
> someone <news...t@gmail.com> wrote:
>
> GNU date can do all sorts of varied date/time calculations, see the -d
> option. For example, if $upsecs is the uptime in seconds "date '+%s'
> -d "now - $upsecs secs"" will calculate the start time in UTC
> seconds.

Interesting, I'll remember that - thanks for that, Jerry...



Icarus Sparry

unread,
Apr 8, 2013, 8:49:47 PM4/8/13
to
On Mon, 08 Apr 2013 10:34:11 -0700, someone wrote:

> On Monday, April 8, 2013 6:41:48 PM UTC+2, Icarus Sparry wrote:
>> On Sun, 07 Apr 2013 17:24:03 -0700, someone wrote:
>>
>>
>> You seem to be saying that the script is run on the NAS box directly,
>> and from the syslog output it looks like the NAS box is running some
>> variant of Unix.
>
> Exactly - some kind of modified linux, I think.
>
>> If you have a /proc/uptime, I would use that. It has two numbers, the
>> first of which is the time the system has been up.
>
> THANKS! Very good idea - MUCH better than parsing log files... I'm very
> grateful. Now I just need to google for how to do some mathematics on
> this value, I would guess other people did this before me and that
> google can help me find a solution.

Something like this should work

U=$(sed 's/\..*//' /proc/uptime) # Get the uptime in seconds

if [[ $U -gt 500 ]]
then echo system has been up for more than 5 minutes
else shutdown -h now
fi

someone

unread,
Apr 9, 2013, 3:10:43 PM4/9/13
to
Yep, google has some examples like that - thank you.

Chris F.A. Johnson

unread,
Apr 9, 2013, 5:05:46 PM4/9/13
to
On 2013-04-09, Icarus Sparry wrote:
> On Mon, 08 Apr 2013 10:34:11 -0700, someone wrote:
>
>> On Monday, April 8, 2013 6:41:48 PM UTC+2, Icarus Sparry wrote:
>>> On Sun, 07 Apr 2013 17:24:03 -0700, someone wrote:
>>>
>>>
>>> You seem to be saying that the script is run on the NAS box directly,
>>> and from the syslog output it looks like the NAS box is running some
>>> variant of Unix.
>>
>> Exactly - some kind of modified linux, I think.
>>
>>> If you have a /proc/uptime, I would use that. It has two numbers, the
>>> first of which is the time the system has been up.
>>
>> THANKS! Very good idea - MUCH better than parsing log files... I'm very
>> grateful. Now I just need to google for how to do some mathematics on
>> this value, I would guess other people did this before me and that
>> google can help me find a solution.
>
> Something like this should work
>
> U=$(sed 's/\..*//' /proc/uptime) # Get the uptime in seconds

No need for sed:

IFS=. read U junk < /proc/uptime

> if [[ $U -gt 500 ]]
> then echo system has been up for more than 5 minutes
> else shutdown -h now
> fi
>


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

someone

unread,
Apr 10, 2013, 1:46:42 PM4/10/13
to cfajo...@gmail.com
On Tuesday, April 9, 2013 11:05:46 PM UTC+2, Chris F.A. Johnson wrote:
> On 2013-04-09, Icarus Sparry wrote:

> > U=$(sed 's/\..*//' /proc/uptime) # Get the uptime in seconds
>
> No need for sed:
>
> IFS=. read U junk < /proc/uptime
>
> > if [[ $U -gt 500 ]]
> > then echo system has been up for more than 5 minutes
> > else shutdown -h now
> > fi

Ok, thank you - always nice to see alternative solutions...

Just an extra "bonus-question":

In my shutdown-script I have something like:

--------------
#!/bin/sh
# touch /tmp/shutdown.now
FILE=/tmp/shutdown.now

if [ -r $FILE ]; then
rm $FILE
date
date >> /tmp/turn_off_diskstation.txt
....
....
....
sleep 10
beep
/sbin/poweroff
fi
--------------

But usually this script is executed from my cron-job (using /etc/crontab). When I look at /tmp/turn_off_diskstation.txt I find that it's just empty (0 bytes). What does it mean?

I think it means that my write cache has not been flushed... Does anyone know how to flush the write cache?

I guess the write cache could be emptied, when the script returns - but it never returns, due to the fact that calling /sbin/poweroff turns off the computer...

Do you know how to solve this? Is it necessary to call another script, which logs to /tmp, just to ensure flushing the write cache?

Thanks...

someone

unread,
Apr 10, 2013, 2:48:37 PM4/10/13
to cfajo...@gmail.com
On Wednesday, April 10, 2013 7:46:42 PM UTC+2, someone wrote:
> Do you know how to solve this? Is it necessary to call another script, which logs to /tmp, just to ensure flushing the write cache?
>
>
> Thanks...

Uh, sorry - I just got a new idea and want to ask an additional question (besides the flush write cache-question above):

Currently, I have to setup power-on-time using this program "Advanced Power Manager", e.g. monday-friday at 1800 hours. Besides that, I have to change crontab and ensure that within an appropriate number of minutes from boot-up-time, run the download-program:

#minute hour mday month wday who command
........... (something) ............
07 18 * * 1-5 root cd /volume1/getData; python data_downloader.py

As you can see, I now have 7 minutes to boot the system (the startup-scripts take about 4 minutes, I think - I added some extra minutes, because maybe in the future with software updates, it'll take longer to boot up).

But this solution is stupid, because I have to change the boot-up time two places and ensure that the crontab-job starts 7 minutes after the power-on-utility wakes up the system...

Here's my idea - new crontab-file:

#minute hour mday month wday who command
........... (something) ............
* * * * 1-5 root cd /volume1/getData; python clever_job.py

This runs every minute. I still need to run "python data_downloader.py" from inside "clever_job.py", but ONLY if a certain condition is met...

Which condition? I found out that there's a file on my system with 2 lines:

-----------------
cat /volume1/@appstore/AdvancedPowerManager/config/default
exp_on=Mon-Fri at 18:00
mode=expert
-----------------

This tells when to wake up by hardware/BIOS clock or something (I guess there's a batteri or maybe it just uses a bit standby-power for this). I think that my "clever_job.py" should run every minute and check the current day and time of the week.

*IF* now/today mets the condition of the first line of the /volume1/@appstore/AdvancedPowerManager/config/default (which is: Mon-Fri at 18:00) and *IF* /proc/uptime indicates that the system has been turned on within maybe 5 minutes, *ONLY* in that case should the "python data_downloader.py" command be issued from cd /volume1/getData...

But the problem is: What if today is tuesday or wednesday? This is not so easy for me to solve, because I guess that first I should convert the string "Mon-Fri" to numbers, i.e. 1-5 and next I need to get todays weekday-number, i.e. Wed = 3:

Wed Apr 10 20:44:39 CEST 2013

After that I need to check if it's true that e.g. Mon < Wed < Fri or (1 < 3 < 5), yes this is true... But Saturday is not true: (1 < 6 < 5)...

Last problem is to add maybe 5 minutes to the string: "18:00" and if "now" is e.g. Wednesday and the time is e.g. "18:03", which is "18:00" < "18:03" < "18:05" AND if the system has just been turned on, then run "python data_downloader.py"...

With this solution, I only have to edit the power-on time from within "Advanced
Power Manager" - can somebody tell me what you think of this and/or maybe also help with code snippets?

I would be grateful for your help, thanks... I think this is the direction for me to go into now...

0 new messages