LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-08-2011, 12:49 PM   #1
IgnitusBoyone
LQ Newbie
 
Registered: Jan 2007
Location: Memphis, TN
Distribution: Gentoo 11
Posts: 13

Rep: Reputation: 0
Calling Crontab over non interactive ssh session


Ok, is there any obvious reason like security permissions why the following script would fail when executed over ssh with a supplied command

File updatecron.sh

Code:
#!/bin/bash
crontab -l > crontab.bak
cp crontab.bak crontab
echo " 30 2 * * * /usr/local/bin/targetprogram 2>&1" >> crontab
crontab crontab
This simple would be called using
Code:
ssh user@host updatecron.sh
The file was put in place prior to the attempt to run it remotly. Essentially I had over 100 servers where I needed to make a crontab update, using a usr who doesn't have admin access. Now the script works in an interactive ssh session just fine, but when ran as stated above in a non interactive mode it produced crontab.bak dorrectly and modified the new crontab file correctly, but the entry in cron was empty. This leads me to believe the line "crontab crontab" failed due to some permission or enviroment issue I am unaware of.

If you have any feedback I would greatly appriciate it and if you have a better idea on how to automate commands like this when pushing to multiple servers I'm all ears.
 
Old 06-08-2011, 12:51 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656
name the file 'newcrontab' instead of crontab

anything in /var/log/messages or /var/log/cron ?

Last edited by szboardstretcher; 06-08-2011 at 12:53 PM.
 
Old 06-08-2011, 01:13 PM   #3
IgnitusBoyone
LQ Newbie
 
Registered: Jan 2007
Location: Memphis, TN
Distribution: Gentoo 11
Posts: 13

Original Poster
Rep: Reputation: 0
I'll test renaming the script in a few minutes and get back to you.

As for as the log request. For some reason the team in charge of the RH images puts var/log as 600 root root making my ability to look at the logs a very leangthy process. I'll work on that because it isn't something I thought about last night when I ran in to the trouble.
 
Old 06-08-2011, 01:26 PM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,338

Rep: Reputation: 259Reputation: 259Reputation: 259
I would assume that the PATH is different whether you get an interactive or batch session. Can you please check whether itís in your PATH also when invoked remotely:
Code:
$ ssh user@host which updatecron.sh
Maybe ~/.bashrc and/or ~/.bash_profile needs to be adjusted.

NB: You can also avoid your own temporary file:
Code:
#!/bin/sh

if [ "$PARAMETER" ]; then
     echo "$PARAMETER" >> $1
else
     export EDITOR=$0
     export PARAMETER="$1"
     crontab -e
fi
and on the local machine then:
Code:
$ ssh user@host /home/user/updatecron.sh '" 30 2 * * * /usr/local/bin/targetprogram 2>&1"'
$1 during the second invocation is the name of the temporary file crontab -e normally feeds to the editor. Instead of the echo used here, you can also make more complex changes using sed or awk.
 
Old 06-08-2011, 04:07 PM   #5
IgnitusBoyone
LQ Newbie
 
Registered: Jan 2007
Location: Memphis, TN
Distribution: Gentoo 11
Posts: 13

Original Poster
Rep: Reputation: 0
You were right. It was a path issue. The file provided need to be explicitly stated. I actually went ahead and used your version for future udpates because it makes it easier. I appriciate the help.

As a second note. I would have never thought about a recursive call to the same script. I like the technique and will need to find more ways to use it in the future.
 
Old 06-09-2011, 04:22 AM   #6
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,338

Rep: Reputation: 259Reputation: 259Reputation: 259
Great that it works now.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SSH concurrent session limit and idle session time out lasygsd Linux - Newbie 3 10-30-2014 07:56 AM
Can't use aliases in non-interactive ssh command openSauce Linux - Software 2 01-08-2009 06:52 AM
How to get a non-interactive ssh connection jahvascriptmaniac Linux - Networking 7 11-18-2007 03:19 AM
[Shell scripting] interactive and crontab pb maxance Programming 4 06-10-2004 12:00 PM
non-interactive ssh podollb Linux - Software 3 04-20-2004 03:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration