Running Cron Job

I am looking at how to run a job at a specified time. I looked up how to use intercom speakers to play music and found a lot of information. I could not find any good information on how to explicity set up a scheduled time to run a system command

So i made a .call file that does exactly what i want if i run command
cp /var/spool/asterisk/tmp/ /var/spool/asterisk/outgoing

it calls the speaker, speaker auto answers, and musiconhold engages.

However i cannot figure out how to get the system to run that command at a designated time like 9:00am.

I have looked up cron jobs, but it is unclear of where script goes to make it activate, and cron seems old and now it is Jobs. I found other information regarding /etc/cron.d folder and files inside there.

I am unable to find a good place to stick this system command call in order to have the jobs scheduler pick it up and do work at the designated time every day

@MDTechTeam yeah, cron is pretty old technology, however it is reliable. The crontab -e command can be used to edit a user’s cron jobs. To run the above command everyday at 9am you can add the following entry to the asterisk user:

0 9 * * * (cp /var/spool/asterisk/tmp/ /var/spool/asterisk/outgoing)

Note: This will only execute at 9am given your system is set to the correct timezone and not UTC. If it’s set to UTC, you’ll need to set it to the UTC time for your timezone.

I hope this helps!

1 Like

don’t use cp, use mv, or it will sometimes fail. Make sure the file is owned by asterisk and use

crontab -e -u asterisk

to add the cronjob

1 Like


I have seen that before however what i am not finding i where do i stick that command?

In what file am i too update?

or from command line I do the following? (re-reading post i feel this is the right answer here)
crontab -e -u asterisk 0 9 * * * (cp …)

crontab -e will open the relevant file for the -u user in /var/spool/cron/ for editing, if it doesn’t exist it will be created.
Each line is self defining, so simply add your line at the bottom, again DO NOT USE cp (copy) use mv (move) or you will experience problems

I am afraid if i use move, every morning i would have to replace the file inside the tmp folder for it to move into the outgoing folder.

Is this a correct assumption?

That is correct, make a copy of the file and move the copy and make sure the ownership is correct as Asterisk will ‘aggressively consume’ files arriving in that directory resulting in failed calls if the read is performed before the write is finished, there is no writing involved in a move, the file instantly appears fully formed.

This seems brutal. I don’t want to keep doing work every morning

Is the fact you are suggesting mv rather than cp a result of crontab or asterisk/outgoing aggression?

Can i set up a crontab to copy the file at 6a then use the 9a call to mv the copied file, persuggestion? or does the crontab action cause copy to fail?

You don’t need any brutal re-work, just a a slightly more complicated cron job

0 9 * * * cp /var/spool/asterisk/tmp/ /tmp && mv /tmp/ /var/spool/asterisk/outgoing

should do.

As to the why’s and the wherefores of mv’ing not cp’ing, I tried to explain the mechanics of how that part of the linux files system works. Probably best to just accept it as a fact and you will be less likely to have failed calls.

appreciate it. i think i understand. The outgoing folder consumes so fast, the copy function is a write and i assume maybe asterisk may consume before write completes. Where as move is more of a pointer association within the folder so it gets dropped all at once ready to be consumed by asterisk complete.

i do appreciate the modified cron command though thanks

To use an emotive but technically correct term, mv’s are atomic (pronounced a-tomic and meaning un-cuttable) , cp’s are not

but in retrospect and to be rigorous ( I try to be) , the cp into /tmp and the mv into …/outgoing would better have the -f (force) option in case anything else eff’ed up ‘the last time around’

0 9 * * * cp -f /var/spool/asterisk/tmp/ /tmp && mv -f /tmp/ /var/spool/asterisk/outgoing

So i got it to work and playing on 1 speaker no problem. Within FreePBX i set up a group for paging, maybe not best way but it seems to work when wanting to broadcast to all speakers.

I associated an extension 9000 to the group so we could just call all speakers through that single extension.

I was using your recommend call file script from another thread.

Channel: PJSIP/9000
Application: musiconhold
Data: musiciwanttoplay,120

I created a musiconhold category called musiciwanttoplay and when I exchange 9000 with 2001. I experience no issue. It plays as expected for the duration I desire.

But when I try and call the group, 9000, I get no activity over the group as I was expecting.

I am assuming that this trick requires a pickup, and extension 9000, since it is a page group, may not in itself initiate a ‘pick up’ for the call file to operate.

What is the preferred method to do this to multiple speakers at all times?

You want

Channel: Local/[email protected]
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.