You are not logged in.

#1 2009-01-09 06:17:50

void.pointer
Member
From: Dallas, TX
Registered: 2008-07-30
Posts: 239

cron issues - Need to setup environment in crontab

Hi,

I'm running some cron jobs as root and while they run fine by manually executing the scripts, they fail when the cron job executes them. This seems to be an environment issue. What can I place in my crontab configuration file to fix this? I know I need to set a couple of variables, such as HOME, SHELL, LOGNAME, etc.

The problem is that I've used ssh-add to register a private key of mine so that when I run SCP it won't prompt me for a passphrase. the 'scp' command gives me the following error when I run it from cronjob:

"Permission denied (publickey,keyboard-interactive)."

help is appreciated.

Offline

#2 2009-01-09 10:48:23

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: cron issues - Need to setup environment in crontab

I didn't fully understand your question. Are you asking how to set environmental variables in crontab? If so, check the man page: http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5.

If the question was "how do I get cron to run with my user settings", it would be helpful if you posted more information. How are you setting environmental vars? How are you editing crontab? How have you configured SCP (maybe remove sensitive data and post the configuration file)?

I also found this while skimming google: http://unix.derkeiler.com/Newsgroups/co … /1045.html

Check if it helps (I only glanced at it, but it seemed to be the same problem).


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#3 2009-01-09 15:36:05

void.pointer
Member
From: Dallas, TX
Registered: 2008-07-30
Posts: 239

Re: cron issues - Need to setup environment in crontab

Sorry I was in a bit of a hurry when I wrote my initial post. I'll try to give a little more details:

The cronjob I'm trying to execute is located in /etc/cron.daily and it has the permissions rwxr-xr-x. The contents of this script are below:

  1 #!/bin/bash -l
  2
  3 echo "Starting backup"
  4
  5 # change to working directory
  6 cd /srv/svn/backup
  7
  8 echo "Changing Working Directory"
  9
 10 # create a timestamp to append to the file name
 11 TIMESTAMP=$(date '+%m%d%y-%H%M%S')
 12
 13 echo "Creating Timestamp"
 14
 15 # dump and gzip the repository
 16 svnadmin dump --quiet /srv/svn/repo/planb > SVN_$TIMESTAMP
 17 echo "Dump complete"
 18 gzip -f SVN_$TIMESTAMP
 19 echo "GZip complete"
 20
 21 # upload the file
 22 scp SVN_$TIMESTAMP.gz user@www.someurl.net:SVN_$TIMESTAMP.gz
 23 echo "SCP complete"
 24
 25 # remove the temporary files
 26 rm SVN_$TIMESTAMP.gz
 27 echo "Removing temp files"

The numbers in the front are the line numbers in VIM. Please ignore those. When I edit my crontab, I run the following command:

$ su -c "crontab -e"

This allows me to edit the crontab file for user 'root', which is the user that currently runs all cronjobs on my system. The specific line in the crontab file for user root that executes the script I posted at the top is below:

03 00 * * * /etc/cron.daily/svn_backup >> /var/log/cron.log 2>&1

The entire contents of my crontab for user root are below:

#
# DO NOT EDIT THIS FILE MANUALLY!! USE crontab -e INSTEAD.
#

# Setup environment

# <minute> <hour> <day> <month> <dow> <command>


01 * * * *  /usr/sbin/run-cron /etc/cron.hourly
00 06 * * * /usr/sbin/run-cron /etc/cron.daily
22 00 * * 0 /usr/sbin/run-cron /etc/cron.weekly
42 00 1 * * /usr/sbin/run-cron /etc/cron.monthly
03 00 * * * /etc/cron.daily/svn_backup >> /var/log/cron.log 2>&1

It's mostly the default stuff. Note that the bottom line in the crontab file is unnecessary, it's only there for testing purposes. When my script works right in cron I'll remove that line. As I've already said, I've already used ssh-add to add my private key to the ssh agent so that I can use SCP without a passphrase. The svn_backup script works perfectly when I run it manually, which leads me to believe that Cron is missing some sort of environment settings that I have when running the script manually.

As for configuring SCP, I'm not sure how to do that. I simply haven't done it. It's using whatever the default configuration is in the pacman package. I can still post it if you really would like to see it.

I hope this information is a little more useful. I appreciate your help.

Last edited by void.pointer (2009-01-09 15:37:40)

Offline

#4 2009-01-09 20:15:37

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: cron issues - Need to setup environment in crontab

Hmm, I've got a similar issue trying to use the ssh-add key which was run as a user and then accessing it from a cron job as root.

However, in your case, what about moving your script from /etc/cron.daily to, say, ~/scripts/svn_backup and then adding an entry (crontab -e) as your user to execute that script daily (03 00 * * * /home/user/scripts/svn_backup >> /var/log/backup.log 2>&1 --- or something like that). That way cron is executing it as your user instead of as root.

I hope I said that right smile Good luck!

Scott

Offline

#5 2009-01-10 03:52:15

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: cron issues - Need to setup environment in crontab

void.pointer wrote:

The problem is that I've used ssh-add to register a private key of mine so that when I run SCP it won't prompt me for a passphrase.

Have you done this as root or as your normal user?
My suspicion is that you've set this up as a normal user and cron, because it's running as root, doesn't know about your user's settings. I don't know how/where the private key is stored and reloaded, but finding it will get you closer to the solution. (just googled a bit, seems it should be in ~/.ssh)

You could try running the cronjob as your local user ("crontab -e", no "su" (I know you know, I add it for clarity)) and see if it behaves as expected. If it does, try copying/moving the key to the corresponding directory in /root/ and check the root cronjob again. (/root/.ssh/ ?)


I don't understand why you've configured your crontab the way you have. With the default lines

01 * * * *  /usr/sbin/run-cron /etc/cron.hourly
00 06 * * * /usr/sbin/run-cron /etc/cron.daily
22 00 * * 0 /usr/sbin/run-cron /etc/cron.weekly
42 00 1 * * /usr/sbin/run-cron /etc/cron.monthly

everything in /etc/cron.daily will get run once daily, including your script. By adding

03 00 * * * /etc/cron.daily/svn_backup >> /var/log/cron.log 2>&1

you're running it twice daily. As you've redirected the output explicitly in that last line, I presume that you would want the output to be redirected when it's run by the daily cronjob. You should remove that script from /etc/cron.daily and either change the line that you added to the crontab or remove it and add this script to the daily directory (which you do depends on when you want it to be run)

#!/bin/bash
/path/to/svn_backup >>/var/log/cron.log 2>&1

My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#6 2009-08-25 15:09:07

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: cron issues - Need to setup environment in crontab

@void.pointer, I recently worked out a similar setup on my system and I think I can talk you through this, if you're still seeking help. But this is now an old thread, so I'll wait for you to respond.

Offline

Board footer

Powered by FluxBB