Submitting cron job through script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Submitting cron job through script
# 1  
Old 09-22-2014
Submitting cron job through script

I would like to run a script, as root, which will eventually set up cron job for a non privilege user. Please advice.
# 2  
Old 09-22-2014
For something easy, it will depend on
1) Has the user the right to use cron?
2) Has the user already a cron file?
# 3  
Old 09-22-2014
Yes, there are the two methods, edit and replace.
I have described them in a previous article
Can cron be modified via a script?
# 4  
Old 09-22-2014
Requirement was: "...will eventually set up cron job for a non privilege user."

As the crontab command has no option to write to another users crontab, it will need to be done like this:
Code:
echo "0 * * * * /path/to/myscript.sh" >> /var/spool/cron/crontabs/someuser


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks
# 5  
Old 09-22-2014
Be aware that many crond servers will only read the files at start of on a signal sent by editing a file with crontab -e. You may need to resort to:-
Code:
crontab -l > /tmp/my_cron
echo "0 * * * * /path/to/myscript.sh" >> /tmp/my_cron
crontab /tmp/my_cron

This will:-
  • list the current jobs for your user
  • append the new record
  • submit the whole lot back again



Robin
This User Gave Thanks to rbatte1 For This Post:
# 6  
Old 09-22-2014
For what its worth, using a script to create a cron job as the root user may lead to some odd, hard to find and possibly dangerous bugs. You may want to rethink your approach.
# 7  
Old 09-22-2014
Quote:
Originally Posted by rbatte1
This will:-
  • list the current jobs for your user
  • append the new record
  • submit the whole lot back again
Doesn't this add a line to root's crontab when run as root?
I understand atanubanerji wants to set up a cron job for a different user (than root).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

How to end script in a cron job?

I've created a script to copy backup files from an HP-UX 11iv3 system to an NFS share on another machine. I want to schedule the script to run via cron. The script is simply three lines of cp /backups/Backup /shared/Backup. I've saved the script as a .sh file and call it with KSH. Do I need to... (3 Replies)
Discussion started by: jduehmig
3 Replies

2. UNIX for Dummies Questions & Answers

Error while running a script through cron job

Hi Team, When i am running the below query manually it is giving me the right output i.e. export PATH=/usr/sbin:/usr/bin:/sbin:/bin:$PATH ADMIN=abc@abc.com CPU_HIGH=`sar|awk '{print $9}'|sort -n|head -5|sed -n 5p` CPU_MAX=`echo "scale=3; 100-$CPU_HIGH" | bc` CPU_LOW=`sar|awk '{print... (13 Replies)
Discussion started by: Ekamjot
13 Replies

3. Shell Programming and Scripting

how do I run bash script using cron job

How do I run bash script using a cron job? I have tried to just write the path of the script, but that didn't work. (1 Reply)
Discussion started by: locoroco
1 Replies

4. Solaris

cron job for phython script

Hello, How do I schedule a cron job for a phython script to run every hour? Also, in case in future I decide to edit/cancel the job how should i do it? Does it matter where my phython script is located? Also, I have am using mailx utility in my script to send me an email and dont want... (7 Replies)
Discussion started by: siddhans
7 Replies

5. UNIX for Dummies Questions & Answers

problem submitting job to queue

Hi, I am trying to submit a job to a queue on a cluster. When I run the job ( python script) from the command line it runs without putting python at the start. The script imports everything from another congifuration file (.config) but when I submit to the queue it tells me there is no module... (0 Replies)
Discussion started by: i-dont-know
0 Replies

6. Shell Programming and Scripting

Behavior of Bad Script in Cron Job

Hi A Ksh script is deployed in a server and executed through cronjob. If one of the line in the middle of the script fails . Are the remaining lines executed ? (3 Replies)
Discussion started by: Sivaswami
3 Replies

7. Shell Programming and Scripting

Cron job shell script..

Hey Guys, i was trying out a shell script which has to remove a file for every 90 mins. this is the code i came up with . $ crontab -e file1 file1 contains 30 1 * * * * rm -r /folder1/folder2/somefile.txt Now i need the cron to run for every 90 mins. the problem with this is... (8 Replies)
Discussion started by: Irishboy24
8 Replies

8. Shell Programming and Scripting

comment out a cron job as part of a script

Greetings, I am creating a ksh script to automate the installation of a utility on many servers. As part of this install, I want to check for a job in root's crontab. If the job exists, I need to comment it out. I know I will need to copy off the crontab then read it back in, but I am... (4 Replies)
Discussion started by: 22blaze
4 Replies

9. UNIX for Advanced & Expert Users

Need help with a script run by a cron job.

Hi, new to this forum and not sure if this is the right place to post. I'm new to cron jobs and scripts, and I need some help with a script to be used with a cron job. I already have a bot set up at a certain website address and need a script that the cron job will load every day that tells it to... (1 Reply)
Discussion started by: klawless
1 Replies

10. UNIX for Advanced & Expert Users

Cron job for Perl script

Although there are many threads on this forum regarding cron, none have specifically answered my question. So hopefully someone can shed some light on what I'm doing wrong.. I have a perl script that I want to run in a cron job. Since I've read that cron doesn't have any environments set, I... (3 Replies)
Discussion started by: man
3 Replies
Login or Register to Ask a Question