-1

I have a bash script which always giving output where I have it redirected to a file, what i'm trying to do is to rotate the redirected file, below is where i start the original script and redirect it:

.
somecode
.    
su - username -c "command >> /path/to/directory/output.txt" &
.
.
code continues..

and below is the crontab i'm trying to create:

cd /path/to/directory/
timestamp=`date "+%Y%m%d"`
mv ./output.txt ./logs/output.txt_$timestamp
touch output.txt
chmod 757 ./output.txt
gzip ./logs/output.txt_$timestamp
find ./logs/output.txt* -type f -mtime +2 | xargs rm

or this one also failed to do the job:

timestamp=`date "+%Y%m%d"`
cp ./output.txt ./logs/output.txt_$timestamp
echo "" > ./output.txt
gzip ./logs/output.txt_$timestamp

in the first code the original script fails and is no longer working, and in the second script it doesn't clean the output.txt file.

Is there a way to do it while keeping the script working ? Note; i'm running Unix Solaris.

Thanks in advance.

  • Does it have logrotate? – 123 Nov 01 '16 at 14:04
  • No it doesn't have it – user104787 Nov 02 '16 at 07:01
  • The only solution that i could find was to have all of the scripts stopped and then do the rotation afterward start the scripts, it would have been betterif there was a way to change the output path while running, but even this way did it as it is script a send an sms if case that something goes down, so a 10 mins downtime while rotation is still acceptable. thanks all for you help – user104787 Nov 02 '16 at 07:03

1 Answers1

0

You might can use following command.

su - username -c "command >> /path/to/directory/output`date "+%Y%m%d"`.txt" &

or this might help you https://linux.die.net/man/8/logrotate

Faizan Younus
  • 793
  • 1
  • 8
  • 13