LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2011, 04:18 AM   #1
nivedhitha
Member
 
Registered: Jul 2005
Posts: 32

Rep: Reputation: 0
How to append a job to a normal user's crontab from a shell script?


Hi All,

I tried to get an answer for this one, but no go. So pls post your suggestions and help me.

On an AIX system, my requirement is to build and run a script as root. This script does multiple actions. One of the tasks is to append a job to a normal users' crontab.

My basic idea is like this:
cronentry="5 * * * * /tmp/dummyscript"
crontab -u myuser -l > /tmp/cronfile
print "$cronentry >> /tmp/cronfile"

Now /tmp/cronfile contains the updated cron file. My question is, how to put this content back into myuser's cron file?.
The constraints are:
1)Other portions of the script needs root privileges. So normal user cannot execute the script.
2)It is not possible to manually edit cronfile, because this and other steps should be part of a big script that would be executed on a number of systems.
3)Root cannot give the command
Code:
crontab [file]
This would "create or replace your crontab file by copying the specified file, or standard input if file is omitted or - is specified as file , into the crontab directory, /var/spool/cron/crontabs. The name of your crontab file in the crontab directory is the same as your effective user name".

This command would modify the crontab of invoking user, which is root in my case. But I want to modify the crontab of 'myuser'.

How to modify the syntax to achieve this?.

As the remaining portions of the script are in ksh, I am not looking at the perl/expect solution.
 
Old 08-24-2011, 08:03 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by nivedhitha View Post
This command would modify the crontab of invoking user, which is root in my case. But I want to modify the crontab of 'myuser'.
You can try su - user -c 'crontab ...'.
Kevin Barry
 
Old 08-25-2011, 08:51 AM   #3
nivedhitha
Member
 
Registered: Jul 2005
Posts: 32

Original Poster
Rep: Reputation: 0
Thank you for the idea, Kevin. But the script gets stuck when I comes to the su part.

I modified it with some if conditions, to understand till what stage the script goes well.

My script is
Code:
TMPFILE=/tmp/cronfile
cronentry="5 * * * * /tmp/dummyscript"
crontab -u myuser -l >$TMPFILE

if [[ $(grep -c $cronentry $TMPFILE) != 1 ]]; then
print "cronentry not found. Adding cronentry now"
print "$cronentry" >> $TMPFILE
else
print "cronentry found"
fi
su - myuser -c 'crontab $TMPFILE'
The output is
Code:
cronentry not found. Adding cronentry now
And then it hangs here. Basically it gets stuck doing nothing.

But cat $TMPFILE does have "5 * * * * /tmp/dummyscript" in it.

Please help me correct this.
 
Old 08-26-2011, 06:20 AM   #4
nivedhitha
Member
 
Registered: Jul 2005
Posts: 32

Original Poster
Rep: Reputation: 0
Hi All,

I need help with this one. Kindly shed some light on this.

Is it hanging because su opens up a new shell?

Thanks in advance.

Last edited by nivedhitha; 08-26-2011 at 07:20 AM. Reason: Added question about reason for hanging
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to run a job as a different user from crontab digitallyamar Linux - Server 2 09-13-2009 06:39 AM
setup crontab job user ust Linux - Newbie 2 07-07-2009 12:08 AM
permission denied in crontab -l in RHEL4,as normal user kcmakwana Red Hat 3 03-12-2008 11:37 PM
crontab job and run on shell sunhui Linux - Software 1 02-13-2007 05:35 AM
adding a job to crontab via shell script tazio Linux - General 7 10-23-2003 02:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:19 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