Search code examples
phpcronunlink

PHP unlink function and cron job


I am using cron job to execute a php file for sending messages. At the end of my php file that is going to be ran by cron job command, I am going to add unlink(__FILE__);.

Does this unlink function remove the current file after sending all messages?

I am not sure that's why I am asking, because sending messages will need some time and I want my file to be deleted after sending all messages and not in the middle of it.

Second; what's happen if there is an error for sending messages? How can I be informed if there was any error? Because the function has deleted the php file already! How can I find out about the errors if there is any?


Solution

  • This is not the way to go. Creating functionality based on removing source code is a road you do not want to follow. One mess up and another file might get deleted, one without a backup (but we all have backups, right? ;) )

    Instead, I suggest you simply update your database. You have your messages stored. After each message is send, you update the database and set sent=1. Next time you run this script, it should ignore all where sent=1 and now you don't send duplicates.
    You can now also, per message, check if there was a error and log it. And if everything went right, you can also update the DB to when the message was sent.


    To answer the actual question: Yes it should. It is probally a timing or a coding error. Which is hard to debug, as the file deletes itself ;)