I have written the shell script below for a lab in my college. It must look at a log file being upgraded frequently from another process and create a number of copies passed at invokation. Here is the code (logrotate.sh):
#!/bin/bash
# Usage:
# logrotate.sh [-n int] [-s signal] logfile
# where:
# int is an optional integer used to make int number of copies of logfile
# signal is the name of signal which shell command fuser must send to the process managing logfile
# this script lacks of a strong parameters checking
NCOPIES=4
LOGSIGNAL=USR1
#use of getopts to parse the arguments
while getopts "n:s:" OPTION ; do
case $OPTION in
n) NCOPIES="$OPTARG"
;;
s) LOGSIGNAL="$OPTARG"
;;
?) printf "Usage: %s [-n copies to keep] [-s signal to send] filename\n" $(basename $0) >&2
exit 1
;;
esac
done
#shift to read the last parameter (logfile)
shift $(($OPTIND - 1))
LOGFILE=$1
#create logfile.2 logfile.3 ... logfile.NCOPIES
for i in `seq $NCOPIES -1 1` ; do
test -f $LOGFILE.$i && mv $LOGFILE.$i $LOGFILE.$[ $i + 1 ]
done
mv $LOGFILE $LOGFILE.1
#sending signal to process which is writing to logfile to keep on writing to $LOGFILE(original name, without any extensions)
fuser -k -"$LOGSIGNAL" $LOGFILE.1
So I wrote two scripts which every second write to the file log:
-the C program (logtest.c):
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd = open("log", O_WRONLY | O_APPEND);
if(fd < 0 ){
printf("Impossible to open file %s.\n", "log");
return -1;
}
for(;;){
if(write(fd, "Ciao bello mio\n", 15) != 15){
write(2, "Error in writing data.\n", 23);
}
sleep(1);
}
close(fd);
exit(0);
}
-and the shell script (logtest.sh):
#! /bin/bash
while true
do
echo $(date) >> log
sleep 1
done
When I launch
./logtest.sh &
./logrotate.sh log
the script logrotate.sh moves all the files with the correct names (log becomes log.1) and send the signal to the process which owns the file log for that moment (so the shell script logtest.sh) which then keeps on writing on the file log. Furthermore, it seems that there is no difference about which signal I send with fuser: it will react always the same way.
However, if I launch
./logtest &
./logrotate.sh log
it happens that the C program logtest receives the signal from the command fuser and then terminates.
My question is: why the two logging programs have different reactions to the signal sent from fuser? I mean, why the schell script keeps on working, whilst the C program terminates?
In the man page of fuser in the RESTRICTIONS section it says
The -k option only works on processes.
Could it be that shell scripts are not considered as real processes in the shell? This would be new for me... I have searched on Internet but no page found about fuser go deeply inside the signalling section.
fuser
work with thisbash
script?"C
program is mentioned, I just explained in one line why IMO this question is on-topic and is not to be closed