Shell script and a log file

The Keeper

Senior member
Mar 27, 2007
291
0
76
I have this shell script scheduled to run periodically doing some maintenance duties. Now, lots of commands have accumulated into this one shell script, including but not limited to database exports, checks, file backups (tar & gzip combination), etc, etc.

Now I really would like to have the shell script to write a log file of everything it does. How should I go around to implement it? All I need is that it writes the output I would see when running it manually in a shell to a log file, no need for fancy time stamping or anything.

Thanks a bunch. :)
 

Netopia

Diamond Member
Oct 9, 1999
4,793
4
81
Perhaps the script command would work for this, but I'm not sure since there is no real terminal output.

Joe
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
How are you running the script? Do you run it manually or is it part of a cron job?
 

crontab

Member
Dec 20, 2000
160
0
0
Couple ways. Look at the first line in the script to see what shell it's going to run it in, i.e: #!/bin/ksh would be an example.

At a prompt, run ksh -x /path/of/script > /tmp/script.out 2>&1 &
or bash -x or sh -x, etc...

If it's a cron, add the same before the call of the command.

You can stick in a set -x after the shell declaration within the script, but I am not sure if you want to edit the script if it's under someone else's version-ing control.
 

The Keeper

Senior member
Mar 27, 2007
291
0
76
crontab, I'm sorry but what you said went over the top of my head. :)
Crusty, it is normally ran by cron. Though some times manually too.
 

Brazen

Diamond Member
Jul 14, 2000
4,259
0
0
I thought cron automatically logged the output of all it's scheduled commands?
 

crontab

Member
Dec 20, 2000
160
0
0
head/more/cat/view the script to see see what shell the script is running in. Just look at the very first line. Assume it says #!/bin/bash

i assume you cron job looks something like this:

0 0 * * * source .an_env; /path/to/the/script

Change it to:

0 0 * * * source .an_env; bash -x /path/to/the/script > /tmp/script_output.txt 2>&1