Hi,
I have seen your requirement. We can set this function in cron, so that it can be executed automatically at a given time. But we need to set crontab also. Because drupal cron runs when someone hits your website. If your website is not getting hitted by someone, then cron will not run. But if cron tab is set then it will run and our custom function will be executed.
Thanks