Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash create file every 10 minutes

Tags:

bash

cat

I want to create a file every 10 minutes, after the file has been created it will be overwritten with a new name by another programme so a new one should be created by the script below:

#!/bin/bash
echo 
while true
do
    cat >> trn 
    echo creating trn
    sleep 600   
done

when I run this, it creates the first file, doesn't echo the statement then stays in a loop without creating any more files.

like image 235
squar_o Avatar asked Oct 11 '25 09:10

squar_o


1 Answers

Do not use cat >> trn for that. It is actually blocking execution of your script (awaiting user input)!

You can use instead (for instance)

  • touch trn or >> trn (will create 'trn' file with empty content, only if doesn't already exist)
  • echo > trn (will create / overwrite 'trn' file with one newline character content)
  • echo -n > trn or printf '' (more portable) or simply > trn (will create / overwrite 'trn' file with empty content)

thanks to @CharlesDuffy for his enlightning comments. Info here on better portability of printf '' over echo -n .


If your system supports watch command, then there is this much simpler option that replaces your whole script (credits to @Qback in comments) :

watch -n 600 touch trn
like image 129
Pac0 Avatar answered Oct 16 '25 10:10

Pac0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!