1

I am pretty new to unix systems and their workings. Is there any way to schedule a cron job in unix which runs on every day 12:00 AM and checks if the day is Thursday it stops a service and if the day is Friday it starts the service again?

4

You're overcomplicating it by trying to make one job that conditionally does two things. You want one job to start the service on Thursday, and another to start it on Friday, as in the following cron table.

0 0 * * 4 service myspiffyservice stop   > /dev/null 2>&1 # stop myspiffyservice on Thursday
0 0 * * 5 service myspiffyservice start  > /dev/null 2>&1 # start myspiffyservice on Friday

If you are talking about executing a job rather than starting or stopping a service, this can also be handled by one cron job that only runs on nonThursdays:

0 0 * * 0-3,5-6 /path/to/myspiffyjob > /dev/null 2>/dev/null # Run spiffy job on non-Thursdays

The above schedule translates to 'At 00:00 on every day-of-week from Sunday through Wednesday and every day-of-week from Friday through Saturday'.

  • Hello thanks for the response, but the scenario is like the service should run on all days except thursday if the day is thursday it should not run, will the above one make it possible ? – Rebbeca Jun 29 '17 at 17:11
  • It will. On Thursday, the service is stopped; on Friday it is started. Which means it is still running on Saturday. Services run continually until stopped. – DopeGhoti Jun 29 '17 at 17:12
  • Thanks, is there any way to use the mail service to know if the service is not running or started? I mean if the cron job is executed successfully and the serivice starts again on friday is there any way to know if the service status is started – Rebbeca Jun 29 '17 at 17:20
  • That's beyond the scope of the question, but you could certainly set up any of a myriad of monitoring tools (e. g. Nagios or monit) to watch the service to ensure it's up and running on the correct schedule and send out notifications for exceptions. – DopeGhoti Jun 29 '17 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.