0

I have this code snippet that needs to execute on 31 Dec 11:59:59 every year. I working in Mean.Js framework so it would be great to have a solution specific to Node.js.

But, even plain Javascript or generic algorithms are also welcome.

Rohit Rane
  • 2,790
  • 6
  • 25
  • 41
  • 2
    i would think a cron job would be a lot more reliable and simple than keeping a node instance up just to check a date all the time... – dandavis Mar 09 '15 at 07:06
  • I agree with @dandavis. Only if you really need to integrate this into a running app, you could use node-cron (https://github.com/ncb000gt/node-cron) - but its way better to use a OS-Cron-Job as it is more likely that your OS is running at exactly that date than your app. – David Losert Mar 09 '15 at 07:08
  • @JackOfAll hope this question might help you http://stackoverflow.com/questions/20499225/i-need-a-nodejs-scheduler-that-allows-for-tasks-at-different-intervals – Ravindra Galav Mar 09 '15 at 07:14
  • @jackOfAll, if my answer below has helped you, please accept it. Thanks. – NarendraSoni Mar 10 '15 at 01:34

2 Answers2

0

If you want to use node.js anyway, you can use cron: simply create a cron job with the desired time, date and your script.

Otherwise, the strategy for doing it would be this:

  1. Determine the current Date
  2. Subtract it from the target Date (31st Dec of the current year (which you need to determine) at 23:59)
  3. Call setTimeout with the time difference

Note for future questions: read the SO help docs. Include what you tried so far and add code if you expect code in the answers.

Tobias
  • 7,723
  • 1
  • 27
  • 44
0

You basically trying to schedule something and for this you don't need any kind of algorithm. Only thing that you need to know is crontab

Note: I am assuming you are working on either Mac or Linux. There must be something like crontab in windows too, do some research.

A crontab is a simple text file with a list of commands meant to be run at specified times. These commands (and their run times) are then controlled by the cron daemon, which executes them in the system background.

The steps you need to follow are as follows:

  1. Create a file crontab.txt (or any name)
  2. Write the command that you want to schedule using Cronjob inside crontab.txt
  3. Point crontab to crontab.txt eg; crontab ~/path-of-file/crontab.txt
  4. type crontab -l (to list all cronjobs scheduled )
  5. type crontab -r (to remove cronjob scheduled)

How to write Cronjob?

<minute> <hour> <month day> <month> <week day> <command to execute>

* * * * * specifies "every minute, every hour, every day, every month, every week"

5 18 2 2 3 specifies the 5th minute or the hour, the 18th hour of the day, the second of the moth , second month (february) or the third week day. So this schedule will run when all this conditions are true i.e.; 6:05 PM on February 2nd

Similarly, 30 2 * * 6 specifies scripts to run every saturday at 2:30 pm.

So that was about cronjob. Let me give you an example.

Let's say you got: cronjob.txt:

* * * * * ~/pathtofile/call_node_program.sh

call_node_program.sh:

node myfile.js

So your call_node_program.sh would do the job for you.

I am leaving on you to do the research and write the pattern for 31 Dec 11:59:59 every year. Cheers.

NarendraSoni
  • 2,210
  • 18
  • 26
  • I tried cron. Apparently it doesnt work on Windows. Let me check on linux and get back. – Rohit Rane Mar 09 '15 at 08:42
  • @jackOfAll The Windows alternative would be to use the [Task Scheduler](http://windows.microsoft.com/en-gb/windows/schedule-task) – Ben Fortune Mar 09 '15 at 09:19
  • Thanks @BenFortune. I don't have any experience of working in windows machine, I will remember this. :) – NarendraSoni Mar 09 '15 at 10:03
  • @jackOfAll that's nice, give it a try. let me know if you face any problem. let me tell you something funny, when i was learning this long time back, i used 'say "hello world!"' command , to get assured that my cronjob was working, because it makes noise, and rest things execute in background. That was fun. You can try it too he he. – NarendraSoni Mar 09 '15 at 10:07