LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Each time a script is called. A log file is created with time and date + Bash Script. (https://www.linuxquestions.org/questions/programming-9/each-time-a-script-is-called-a-log-file-is-created-with-time-and-date-bash-script-4175421989/)

y0_gesh 08-14-2012 05:05 AM

Each time a script is called. A log file is created with time and date + Bash Script.
 
Wrote a Script to kill (with all zombie process) and start asterisk where users having issue with the service could use a button on a certain webpage to restart asterisk. Am cool with it and am not having any issue with..What i would really like to do now is,.each time someone will use that button to restart the service a log should be created with time and date.

Anyone got an idea how to make this work ?

zhjim 08-14-2012 05:33 AM

If reading correct you allready have script that restarts asterix. Why not write the log there? Or you could use the script of asterix itselfs to do the logging.

What language is your script in?
The start-stop script should be plain bash and should easily be modified.

y0_gesh 08-16-2012 02:28 AM

Quote:

Originally Posted by zhjim (Post 4753804)
If reading correct you allready have script that restarts asterix. Why not write the log there? Or you could use the script of asterix itselfs to do the logging.

What language is your script in?
The start-stop script should be plain bash and should easily be modified.

Sorry for the late reply zhjim,

Am actually using simple bash to write. Well giving more details is that i got 2 script, one to kill asterisk and another to start. U must be thinking why i got 2 script when i can do both actions in only one script. Well i tried but when i ran the script it only killed asterisk. It won't start. So what happens now is that when someone uses the button on the web page. it first calls the kill-asterisk script then start-asterisk script. And this does work!

zhjim 08-16-2012 06:32 AM

You could call the start asterix script a the end of the stop script.
As a reference how to do both in one script check out the scripts in /etc/init.d. They are called something like this
Code:

Usege: prog_name start | stop | restart
(Dunno if you are familary with this form of notation. | pipesymbol means or. So you can call the program like prog_name start or prog_name stop or prog_name restart.)

Heres a example script.

Code:

function start_asterix {
        your way of starting asterix
        if [ $? -eq 0 ]; then
                echo date 'asterix started' >> a_log_file
        fi
}
function stop_asterix{
        your way of stoping asterix
        if [ $? -eq 0 ]; then
                echo date 'asterix stopped' >> a_log_file
        fi
}
case "$ACTION" in
        start)  start_axterix
                ;;
        stop)  stop_asterix
                ;;
        restart) stop_asterix
                start_asterix
                ;;
        *)      echo "Usage $0 start | stop | restart"
esac


theNbomr 08-16-2012 03:11 PM

I think that should really be:
Code:

function start_asterix {
        your way of starting asterix
        if [ $? -eq 0 ]; then
                echo $(date) 'asterix started' >> a_log_file
        fi
}
function stop_asterix{
        your way of stoping asterix
        if [ $? -eq 0 ]; then
                echo $(date) 'asterix stopped' >> a_log_file
        fi
}

Otherwise, looks like exactly what the doctor ordered.

--- rod.

Reuti 08-16-2012 04:11 PM

Besides using a dedicated logfile at this stage, it could also be generated as an entry in the system syslog facility and handled later on by syslog-ng or alike to split it again to different logfiles or forward it to a remote syslog server:
Code:

$ logger -t asterix -p daemon.info started
$ logger -t asterix -p daemon.info stopped

to generate:
Code:

Aug 16 23:00:59 the_hostname asterix: started
Aug 16 23:07:33 the_hostname asterix: stopped


zhjim 08-17-2012 03:16 AM

You're totaly right Nbomer.
Nice stuff as well Reuti.


All times are GMT -5. The time now is 04:55 PM.