Submitting cron job through script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Submitting cron job through script
# 8  
Old 09-22-2014
Okay. I hadn't read that requirement. I would do something like:-
Code:
su username -c "crontab -l > /tmp/my_cron
echo "0 * * * * /path/to/myscript.sh" >> /tmp/my_cron
crontab /tmp/my_cron"

This switches to the non-root account to do the work. The user would have to be allowed to use crontab and not be locked into an application menu else you might get very odd results.




Robin
# 9  
Old 09-22-2014
Quote:
Originally Posted by gandolf989
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.
I think the approach is OK. Maybe thread-o/p is trying to set up a mechanism to deploy a cron job for a lot of systems: think, for instance, of a database backup script to be run as the DBA user. If i had to create such a system i would write exactly such a (postinstall-) script and put it into a package, along with the script to be called from cron itself.

For cron to become aware of the changed crontab you just need to send it a HUP signal:

Code:
echo "0 * * * * /path/to/myscript.sh 2>/dev/null 1>/dev/null " >> /var/spool/cron/crontabs/username
kill -1 $(cat /var/run/crond.pid)

but even if this works i'd still prefer rbatte1's solution because this will work even when the involved path for the crontabs and/or the location of the PID-file changes with different cron-implementations.

Btw.: i suggest to add redirections to /dev/null (or designated log files) otherwise cron would start to mail stray output to the user. Another thing: check if the user is allowed to have a crontab. In most implementations there is a file /etc/cron.allow and another file /etc/cron.deny.

I hope this helps.

bakunin
# 10  
Old 09-23-2014
Thank you very much guys for sharing your thoughts. This time I am using Rbatte1's solution.

Cheer.
 
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