How to end script in a cron job?


 
Thread Tools Search this Thread
Operating Systems HP-UX How to end script in a cron job?
# 1  
Old 09-26-2019
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 include an end or exit command at the end of the script to ensure it exits cleanly?
Thanks,
Joe

Last edited by Peasant; 09-27-2019 at 01:01 AM.. Reason: Added icode tags.
# 2  
Old 09-26-2019
It will exit by itself once done, but you probably want an 'exit 0' at the end in case any of the cp commands couldn't copy.

Cron also has a useful feature where any output from the command gets emailed to you. Generally you want no output for successful commands, but if the command fails, you want cron to tell you about that. So:
Code:
#!/bin/ksh

# Redirect error messages so cron can send them to you
exec 2>&1 

cp source1 dest1
cp source2 dest2
cp source3 dest3

exit 0

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-01-2019
Thank you for the great tips. I've added your suggestions to my script and I'll check on it tomorrow.
Joe
# 4  
Old 10-01-2019
IMHO cron merges stdout and stderr by default.
So there is no need for exec 2>&1.
These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

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. (9 Replies)
Discussion started by: atanubanerji
9 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. UNIX for Dummies Questions & Answers

Setup a cron job and specified the start and end time

Hi guys, How can I specify the start and end time of a cron job. And my start time and end time are specified by minutes. For example, I want to set up a cron runs every 3 minutes from 18:40 to midnight. How can i do this please? Many thanks Best regards, Clu (4 Replies)
Discussion started by: clu
4 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. Shell Programming and Scripting

Running script file using cron job every 5 second

Hi All, I beginner in unix, i have no idea how to set the script file using cron job every 5 second. I also want to execute automatically the output to text file.This is my script name countsys.sh and my textfile abc.txt. (6 Replies)
Discussion started by: mastercar
6 Replies

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

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

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

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

10. UNIX for Dummies Questions & Answers

setting up cron job for month end week report

Hi all, Needs your help in scheduling a cron job for the below mentioned requirement. Just let me know if anybody has a similar job running as mentioned below: Month end reports - Report of all items created in a parent table that were created during the week. Presently this report runs... (3 Replies)
Discussion started by: Bhups
3 Replies
Login or Register to Ask a Question