Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a date time stamp to a logfile in a startup script

Status
Not open for further replies.

wags2272

Technical User
Jun 3, 2005
39
US
I'm working on setting up an RTI license server to auto start on bootup. I've got that working, but it writes a new boot.log file every time overwriting the old one as it does. I'd like to have it create a new log file each time so that I can have some history when troubleshooting.

I've tried implementing a shell variable to create a logfile with the date in it, but am not having any luck getting it to work.

I want the boot.log file to be replaced with a file with a name like: log-Aug-12-08.

Thanks for the help: See file with my trials at the top, but functioning normally below.

Here is what I've got:
#!/bin/sh
#!/bin/csh
#


/bin/su rtiserver -c set tds = `date +"%b-%d-%y"`
echo $tds
/bin/su rtiserver -c set logfile = "log-$tds.log"
echo $logfile
/bin/su rtiserver -c 'echo starting lmgrd > \
/home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/boot.log'
/usr/bin/nohup /bin/su rtiserver -c 'umask 022; \
/home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/lmgrd -c \
/home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/server.lic >> \
/home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/boot.log'
/bin/su rtiserver -c 'echo sleep 5 >> \
/home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/boot.log'
/bin/sleep 5
/bin/su rtiserver -c 'echo lmdiag >>\
/home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/boot.log'
/bin/su rtiserver -c '/home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3lmdiag -n -c\
/home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/server.lic >> \
/home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/boot.log'
/bin/su rtiserver -c 'echo exiting >>\
/home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/boot.log'
touch /var/lock/subsys/local

 
Why all those /bin/su rtiserver ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is a script run at system startup. No paths are assumed at boot so all paths are specified in full. The su specifies to run the command as the specified user rather than root.
 
It would be simpler to write a single script launched with su in the startup script.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The original script was taken from the FLEXlm manual. I'm just trying to modify it to give me a new log file each time rather than overwrite the old one.

 
Replace this:
/bin/su rtiserver -c set tds = `date +"%b-%d-%y"`
/bin/su rtiserver -c set logfile = "log-$tds.log"
with this:
tds=`date +"%b-%d-%y"`
logfile="log-$tds.log"

Then use $logfile instead of boot.log and use double quotes instead of single.


Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
No luck. I get the following errors:

tds: command not found
logfile: command not found
logfile: undefined variable (multiple postings).
 
What is your actual code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
What I posted is the contents of the rc.local file that is run at boot and starts the license server.
 
You're suggestion to use just the variables without the implicit directory or change of user didn't work. It left me with the error messages that I posted.

As I see it, the issue is to set shell variables for the csh to use when the script is executed. In my initial attempts using the "set" command I was able to get a value for the tds variable. The problem I then had was creating the logfile using tds and being able to write to the file.

As you can see from my first post, I was outputting the contents of tds and logfile. tds was correct, but logfile was failing. If the defining of logfile can be solved, I think it will be the answer to my problem.

Thanks for the help.
 
The suggestion PHV made would work under /bin/sh (i.e. Bourne shell), not csh, which was a safe assumption for him to make based on the first line of the script you posted. The second line (#!/bin/csh) has no meaning in that position.

Since run-time startup scripts are generally run under sh anyway you should write them for that shell.

I would write it something like this:

Code:
#!/bin/sh

tds=`date +"%b-%d-%y"`
echo $tds
logfile="log-$tds.log"
echo $logfile
/bin/su rtiserver -c '
        (
                echo starting lmgrd
                umask 022
                /home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/lmgrd -c \
                        /home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/server.lic
                echo sleep 5
                sleep 5
                echo lmdiag
                /home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3lmdiag -n -c \
                        /home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/server.lic
                echo exiting
        ) >> /home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/$logfile'
'
touch /var/lock/subsys/local

You may notice I've removed the nohup. I'm not sure exactly which process here is a daemon, but presuming one of them is you probably need to launch it in the background unless it backgrounds itself (i.e. by putting "&" on the end of the command-line). You may or may not need to nohup it as well.

Annihilannic.
 
Oops! 3rd last line should be:

Code:
        ) >> /home/rtiserver/FLEXlm-Linux-rhe4.0-i386-gcc-3.4.3/'$logfile'

Annihilannic.
 
The user rtiserver uses the csh, hence any variables used by rtiserver have to be defined in csh. (At least I think it has to be that way).

I received some assistance from a colleague, here is what he came up with. It's working, but still generating a couple of errors.

#!/bin/csh


set value = `date +%d-%m-%Y-%k-%S`

echo $value

set oldLogName = "log-$value.txt"

set command = "/bin/cp /home/jkaplan/original.log /home/jkaplan/$oldLogName"

echo $command
/bin/su jkaplan -c "$command"

Thanks for the responses. Comment if you like, but consider the case closed.

 
Have you tested this yet under a boot-up situation, or just by running it manually? If you haven't tried a boot-up, do so... because as I mentioned earlier on some systems all startup scripts are run using /bin/sh regardless of which shebang (#!...) line you have at the beginning of the script.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top