# How to end script in a cron job?

**URL:** <https://community.unix.com/t/how-to-end-script-in-a-cron-job/376336>\
**Category:** HP-UX\
**Created:** [September 26, 2019, 11:37am UTC](https://community.unix.com/t/how-to-end-script-in-a-cron-job/376336 "2019-09-26T11:37:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jduehmig](https://community.unix.com/letter_avatar/jduehmig/32/6_449c533f586939b6e5f246632a46421f.png) [@jduehmig](https://community.unix.com/u/jduehmig)\
**Post date:** [September 26, 2019, 11:37am UTC](https://community.unix.com/t/how-to-end-script-in-a-cron-job/376336/1 "2019-09-26T11:37:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 26, 2019, 12:04pm UTC](https://community.unix.com/t/how-to-end-script-in-a-cron-job/376336/2 "2019-09-26T12:04:09Z")

</div>

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:

```nohighlight
#!/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

```

---

<div class="post-metadata">

**Author:** ![jduehmig](https://community.unix.com/letter_avatar/jduehmig/32/6_449c533f586939b6e5f246632a46421f.png) [@jduehmig](https://community.unix.com/u/jduehmig)\
**Post date:** [October 1, 2019, 8:06am UTC](https://community.unix.com/t/how-to-end-script-in-a-cron-job/376336/3 "2019-10-01T08:06:27Z")

</div>

Thank you for the great tips. I've added your suggestions to my script and I'll check on it tomorrow.  
Joe

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [October 1, 2019, 8:34am UTC](https://community.unix.com/t/how-to-end-script-in-a-cron-job/376336/4 "2019-10-01T08:34:43Z")

</div>

IMHO cron merges stdout and stderr by default.  
So there is no need for `exec 2>&1` .
