How to pipe commands for a cron job?

Forum rules
Before you post please read how to get help
User avatar
mzsade
Level 5
Level 5
Posts: 750
Joined: Sun Jul 19, 2009 4:36 am

How to pipe commands for a cron job?

Postby mzsade » Sat Dec 17, 2011 7:02 am

Hi i want to..at least that's what i think i want to do..sudo gedit /etc/crontab so as to pipe the output of "date" to the input of "date --set" and the resulting output to the input of "hwclock --systohc".

Is this right, for 00:00 hrs. every 15th of December for every day of the week?

Code: Select all

0 0 15 12 *  root /usr/bin/date | date --set | hwclock --set --date


Will the cron job be aborted for a particular day if the computer happens to be powered off at the scheduled time?
Linux User #481272 Reg: 15th Sept., 2008

noodlyappendage
Level 1
Level 1
Posts: 28
Joined: Fri Dec 16, 2011 7:35 pm
Location: Houston, TX

Re: How to pipe commands for a cron job?

Postby noodlyappendage » Sat Dec 17, 2011 9:04 am

I don't believe you can use date -set that way. What is it that you're trying to do?
AFAIK, cron requires the machine to be up to run the command. If you need this, you might look at anacron instead.

User avatar
Habitual
Level 12
Level 12
Posts: 4097
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

Re: How to pipe commands for a cron job?

Postby Habitual » Sat Dec 17, 2011 12:04 pm

mzsade wrote:...Is this right, for 00:00 hrs. every 15th of December for every day of the week?

Code: Select all

0 0 15 12 *  root /usr/bin/date | date --set | hwclock --set --date


It looks correct but I'd /path/to/date and /path/to/hwclock :wink:

Also, you CAN use pipes this way but it may have unpredictable results, so I'd recommend

Code: Select all

#!/bin/bash
/usr/bin/date | /usr/bin/date --set | /usr/bin/hwclock --set --date

Your path to hwclock may not actually be /usr/bin/hwclock, so check. :)

and call it in cron with

Code: Select all

0 0 15 12 *  root /path/to/your/script.sh


mzsade wrote:Will the cron job be aborted for a particular day if the computer happens to be powered off at the scheduled time?

Yes.

If it gives you any grief, run the /path/to/your/script.sh from terminal and see if it does the job correctly/as expected, if not
modify the script slightly with

Code: Select all

#!/bin/bash
set -x
/usr/bin/date | /usr/bin/date --set | /usr/bin/hwclock --set --date

and run that in terminal and it will show you "more" (errors etc)..

HTH.
Windows assumes you're an idiot.
Linux demands proof of it.

noodlyappendage
Level 1
Level 1
Posts: 28
Joined: Fri Dec 16, 2011 7:35 pm
Location: Houston, TX

Re: How to pipe commands for a cron job?

Postby noodlyappendage » Sat Dec 17, 2011 8:05 pm

If I try

Code: Select all

date | date --set

on the command line, I get

Code: Select all

date: option '--set' requires an argument

which makes sense since the format for setting a date is

Code: Select all

date --set=STRING

per the date manpage, so I don't think this pipeline can work as written.

User avatar
Habitual
Level 12
Level 12
Posts: 4097
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

Re: How to pipe commands for a cron job?

Postby Habitual » Sat Dec 17, 2011 10:02 pm

This may help...
http://www.cyberciti.biz/faq/howto-set- ... nd-prompt/

What's wrong with using an NTP Server to set the clock/date?
Windows assumes you're an idiot.
Linux demands proof of it.

User avatar
mzsade
Level 5
Level 5
Posts: 750
Joined: Sun Jul 19, 2009 4:36 am

Re: How to pipe commands for a cron job?

Postby mzsade » Sat Dec 17, 2011 10:14 pm

Habitual wrote:This may help...
http://www.cyberciti.biz/faq/howto-set- ... nd-prompt/

What's wrong with using an NTP Server to set the clock/date?


Thanks, that's a very useful link to explore. What i intend is to schedule synchronization of h/w and system clocks with the output of "date" on given days. Now this gui based app, system-date-admin does it very well, only i don't know how to use cron with it.
Linux User #481272 Reg: 15th Sept., 2008

noodlyappendage
Level 1
Level 1
Posts: 28
Joined: Fri Dec 16, 2011 7:35 pm
Location: Houston, TX

Re: How to pipe commands for a cron job?

Postby noodlyappendage » Sat Dec 17, 2011 11:42 pm

If you have NTP working, do you really need to synchronize the hw clock?
But if you do need to, is

Code: Select all

hwclock --systohc

what you are looking for?

User avatar
mzsade
Level 5
Level 5
Posts: 750
Joined: Sun Jul 19, 2009 4:36 am

Re: How to pipe commands for a cron job?

Postby mzsade » Mon Dec 19, 2011 7:51 pm

How do i specify NTP servers, and how do i use this "Date and time" app in a cron job?
Attachments
Screenshot-2.jpeg
Screenshot-2.jpeg (43.85 KiB) Viewed 1672 times
Linux User #481272 Reg: 15th Sept., 2008

noodlyappendage
Level 1
Level 1
Posts: 28
Joined: Fri Dec 16, 2011 7:35 pm
Location: Houston, TX

Re: How to pipe commands for a cron job?

Postby noodlyappendage » Tue Dec 20, 2011 12:00 am

Here is a page with a list of NTP servers for India.
http://www.pool.ntp.org/zone/in

NTP will periodically update your system clock to match a canonical source clock, so that your system time is up to date. This is handled automatically by NTP without you having to enter a cron job.
Now, supposing your system clock is up to date, is it really important to keep the hardware clock up to date? In my experience, NTP is sufficient. I think that when you resume from suspend, or boot, ntp will update the system clock as soon as there's network access, so the amount of time you are relying on the hardware clock is really very small. However, if you decide you really want to sync the hardware clock, then adding a cron job that does

Code: Select all

hwclock --systohc

should take care of it.


Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 2 guests