How to pipe commands for a cron job?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
mzsade
Level 5
Level 5
Posts: 776
Joined: Sun Jul 19, 2009 4:36 am

How to pipe commands for a cron job?

Post by mzsade »

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?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Linux User #481272 Reg: 15th Sept., 2008
noodlyappendage

Re: How to pipe commands for a cron job?

Post by noodlyappendage »

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.
Habitual

Re: How to pipe commands for a cron job?

Post by Habitual »

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.
noodlyappendage

Re: How to pipe commands for a cron job?

Post by noodlyappendage »

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.
Habitual

Re: How to pipe commands for a cron job?

Post by Habitual »

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?
User avatar
mzsade
Level 5
Level 5
Posts: 776
Joined: Sun Jul 19, 2009 4:36 am

Re: How to pipe commands for a cron job?

Post by mzsade »

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

Re: How to pipe commands for a cron job?

Post by noodlyappendage »

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: 776
Joined: Sun Jul 19, 2009 4:36 am

Re: How to pipe commands for a cron job?

Post by mzsade »

How do i specify NTP servers, and how do i use this "Date and time" app in a cron job?
Linux User #481272 Reg: 15th Sept., 2008
noodlyappendage

Re: How to pipe commands for a cron job?

Post by noodlyappendage »

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.
Locked

Return to “Scripts & Bash”