Copy a file every 60 seconds bash

10,167

Solution 1

To answer the general question, two ways to do this, put it in a while/sleep loop or use a crontab

1) while/sleep

#!/bin/bash
while true; do
  cp -f /customTemplates/login.tpl /www/img/templates/adm/login.tpl
  sleep 60
done

2) crontab (preferred)

Run crontab -e and put the following line there

* * * * * cp -f /customTemplates/login.tpl /www/img/templates/adm/login.tpl

This will run the command every minute of every hour of every day of every month of every day of the week. (ergo every 60 seconds)

But, as Aaron Digulla said it would be better to get to where it's pulling the config from and edit it there, rather then overwriting it every 60 seconds.

Solution 2

A better solution would be to find out where the NAS takes the original file from.

Execute this command to find possible candidates:

find / -name "login.tpl"

You can also use patterns: "*login*"

I suggest to use the quotes when you use patterns, otherwise it might match files in the local folder and the shell will then replace the name before it passes the argument to find.

To find out who changes the file, use auditctl (see this question).

Share:
10,167
Shannon Hochkins
Author by

Shannon Hochkins

Passionate about learning new things, constantly working on my own projects to achieve that goal.

Updated on June 05, 2022

Comments

  • Shannon Hochkins
    Shannon Hochkins almost 2 years

    I have a thecus nas server, and they seem to do some tricky things to their templates to display their files, currently at boot I'm running a shell command to copy one file over another, so that It boots with my custom template, however after a certain amount of time (I'm not sure what this time is) it overwrites it again with the original and my custom template is gone.

    Here is my current boot script:

    #!/bin/bash
    cp /customTemplates/login.tpl /www/img/templates/adm/login.tpl
    

    Is there a way, to perform that copy command, say every 60 seconds? the login.tpl file is only 2kb, so I wouldn't think this could cause any problems.

    Is there anything wrong with doing this, this way? Or is there another trick I could use?

  • Shannon Hochkins
    Shannon Hochkins over 10 years
    I am pretty sure I have done this from the root already, but I'll let it go over night and hopefully it returns more results.
  • Reinstate Monica Please
    Reinstate Monica Please over 10 years
    @ShannonHochkins Yes, assuming it has the same name.
  • Shannon Hochkins
    Shannon Hochkins about 10 years
    The cronjob worked fine! I also didn't know you could just put the cp command straight in there so you taught me something else, thankyou!
  • Manfredo
    Manfredo almost 7 years
    I'm not sure about the crontab but the while loop will copy every 60s + the time needed to execute the cp command (which can take much longer...)