Re: ssh and crontab problem, variable expansion (132 Views)
Reply
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 1 of 9 (203 Views)

install cron issue

[ Edited ]
ssh -n host1 "crontab << */10 * * * * /usr/local/script.sh > /logs/output.${HOSTNAME}"

 

I have around 100 hosts and need to populate this cron entry on them but the variable $HOSTNAME is expanding to the name of the admin box I am trying to deploy from.

 

How can I fix this. Please advise.

 

Thanks,

Allan.

 

 

P.S. This thread has been moevd from HP-UX > Languages and Scripting to Linux > sysadmin - Hp Forums Moderator

 

 

 

 

 

Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,631
Registered: ‎06-21-2000
Message 2 of 9 (195 Views)

Re: install cron issue

I would use $(hostname) rather that ${HOSTNAME}.  $(hostname) will run the hostname command on the serve that the script runs on. 

 

Also, if you are deploying this to HP-UX servers, the */10 syntax will not work.  That is Linux ONLY syntax.  If you want the script to run every 10 minutes in HP-UX you need to specify 0,10,20,30,40,50 for the minutes.

Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 3 of 9 (186 Views)

Re: install cron issue

This is linux I am afraid, the $(hostname) doesnt work on it.

 

Thanks,

Allan.

Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 4 of 9 (170 Views)

Re: install cron issue

even with single quotes I am getting -

ssh -n host1 echo '*/10 * * * * /usr/local/script.sh > /logs/output.${HOSTNAME}' >> /tmp/cron-list

HOSTNAME: Undefined variable.
Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 5 of 9 (161 Views)

Re: install cron issue

Its also not liking the redirection, I am really dead in water, please help!
Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 6 of 9 (151 Views)

Re: install cron issue

ssh -n user@host1 "echo */10 * * * * /script.sh '>' /logs/output.\$HOSTNAME '>' /tmp/cron-list"

HOSTNAME: Undefined variable.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,385
Registered: ‎03-06-2006
Message 7 of 9 (144 Views)

Re: ssh and crontab problem, variable expansion

[ Edited ]

>the variable $HOSTNAME is expanding to the name of the admin box I am trying to deploy from.

 

Then you need to use single quotes:

ssh -n host1 'crontab << */10 * * * * /usr/local/script.sh > /logs/output.${HOSTNAME}'

Also, I assume this << syntax is some bashism?  I guess it isn't.  You would need to set up a here doc.

 

If single quotes doesn't work, it's because $HOSTNAME isn't defined by default.

So you could do this:

ssh -n user@host1 "echo '*/10 * * * * /script.sh > /logs/output.host1' | crontab"

Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 8 of 9 (137 Views)

Re: install cron issue, variable expansion

[ Edited ]

Thanks Dennis that worked though I figured out the solution before I saw your reply.

Another issue I wanted to solve is that  I dont have sshkeyless setup to the hosts and dont have expect installed on the admin box, how do I remote ssh without(or automatically) feeding in the password each time for all 100 hosts?

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,385
Registered: ‎03-06-2006
Message 9 of 9 (132 Views)

Re: ssh and crontab problem, variable expansion

>I don't have sshkeyless setup to the hosts

 

(Did you mean password less?)

I would spend time setting this up so you don't have to do it again.

(Unless you have some management or security reason for not doing it.)-:

 

>don't have expect installed on the admin box, how do I remote ssh without (or automatically) feeding in the password each time for all 100 hosts?

 

Well that's what ssh passwordless is for.

Without expect, I'm not sure there is any other way.

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation