Cronjob

Hi,

I'm totally new to shell scripting. I need help in my crontab script. I'm trying to read some values from user (username, log file directory, server) and then use those value to create a crontab for log rotation after some interval of time.

creating crontabs manually isn't big deal. Infact I have made crontab file, in which i replace the info with the new one.

Can any of you help in this regards as to how to replace the input values in the code that I have and then running a crontab command such that it has all the necessary info ?

Please a little help. :frowning:

You can create a script dynamically using all the parameters you require.

schedule this script in the cron job.

step 1 cron script test.sh in crontab
step 2 create scripts which runs impoting whatever paratemeters or variables are required.

so test.sh may have something like

username=who am i|tr ' ' ''|cut -d ' ' -f1

Please illustrate if you have any specific requirement

Regards

w020637

deleted

Duplicate post... pls read the rules...

@ w020637

It would be more helpful if you explained with a little example.
Thanks.

MisterKhan

Crontab is primarily used for scheduling jobs. If it a simple one liner you can embedd in the crontab but if its a set of tasks then it would be better to put it in a shell script. So what ever task need to be accomplished need to be done in another shell script test.sh which will be scheduled in the crontab. So

step 1 cron script test.sh in crontab
30 18 * * * /test.sh

step 2 create script which runs impoting whatever paratemeters or variables are required.

so test.sh may have something like

username=who am i|tr ' ' ''|cut -d ' ' -f1

Regards

Okay ... I think I'm getting a hold of scripting now .... I've created some script which does what I want ... but not fully. Here is the summary.

[assumption: script is run as root.]
*I have created an input (or properties) file ... which asks the user to input some information such as user name, log directory path, server name etc.
*Then, it substitutes these input values from the user to another file, say, cron.txt.

  • next step is to copy this file from the root to the user's directory (e.g. user1) and then change the permission of that file.
  • last step is to do "crontab cron.txt".

I'm facing problem in the last step. Instead of doing this command in the user's home directory, it makes the changes in the root's crontab ...

I hope someone gives me a suggestion ... Please help!

Thanks.

MisterKhan

To edit a users crontab, you need to add "-u username" to the command.

@ PWSwebmaster

Can you give an example for this.

I'm getting an error as follows:

crontab: proper usage is:
crontab [file | -e | -l | -r ] [user]

lets say, I have a file called "cron.txt" and I am running the script as root. This cron.txt has been copied to the user "user123" home directory.

should the command look like this??

$ crontab -u user123 cron.txt

was the above command the right way ?????

Please help

The -e is also required to do editing.

Try
crontab -e -u user123 cron.txt

Sorry, the -e is not used to use a file for the cron settings.

Your
crontab -u user123 cron.txt
should work

Thanks very much ...