# send jobs to cron in batches



## klabacita (Mar 26, 2012)

Hi guys.

 I have searched but still don't see how can I add jobs to cron in a batch?

 Free*BSD* 8.x, thanks


----------



## DutchDaemon (Mar 26, 2012)

Can you give an example of what you're trying to achieve?


----------



## kpa (Mar 26, 2012)

Look at at(1), cron(8) is ill suited for such a task because you're required to edit crontab(5) file to add the tasks.

(I'm assuming you want to run a number of tasks at certain times but only once)


----------



## fluca1978 (Mar 26, 2012)

If you mean "having a batch script that adds cron entries" then you have the option of editing /etc/crontab or placing a line into /var/cron/tabs/<username>. Both requires your script to be smart enough to write a line into the file with the right syntax and to avoid adding the same entry again and again (a grep(1) is recommended before adding the line).


----------



## klabacita (Mar 26, 2012)

Hi guys.

Yes, I want to run a task only once.

I'm working with pfsense 2.0.1, for some strange reasons squid needs to be restarted, because [cmd=]squid -k rotate[/cmd] won't work if we don't change the logs files' rights to 777 and restart the services, this is nanobsd.

I had asked there but no one since to answer, then I want to achieve this using other tools like 'at' or 'cron'.

Now, I was trying to use at, but the command is not working neither, I had asked again but I still don't had any answer.

Them my brain says, "What else can we do"? and I say what about if I set up a batch to run at start, that batch will send the job to cron and will run at 23:00, later other batch to remove the job at 23:10 and done, this will be same day the system start only.

This is the reason, thanks


----------



## Morte (Mar 26, 2012)

This sounds like the same problem I had with squid.  I'm assumng there's a better way to do this, but my solution was to use cron to call up a script which does the rotation stuff. This is a bash script so you may need adjustments. (cat is substituted for grep in my actual script because I filter some things out).  This might help you until you get a better answer.


```
cat /var/log/squid/access.log > /db/home/squid-logs/$date.log

echo -n '' > /var/log/squid/access.log
chown squid:squid /var/log/squid/access.log

squid -k rotate

bzip2 -9f  /db/home/squid-logs/$date.log
```


----------



## SirDice (Mar 26, 2012)

Use newsyslog(8).

http://jamesoff.net/site/reference/squid-log-rotation-with-newsyslog/


----------



## klabacita (Mar 26, 2012)

Hey, your solution is working right? Then I will try, thanks.

SirDice I have not tried newsyslog, I will give it a try.

My idea was to run a job only once, every time the system restart for any reason, with the command "at" put a job that will restart the service at 11PM and done. Once *yo*u restart the service the log rotation start working.

Good to come here with *yo*u guys, I let *yo*u know my results, thanks again.


----------



## klabacita (Mar 27, 2012)

newsyslog cannot be use in pfsense, pfsense uses clog. I try your solution Morte and works, thanks. But what about the main question, how to add a job to cron in a batch?

Thanks all for your help :e


----------



## Morte (Mar 27, 2012)

If you want to add jobs to run only once (batches or not) that is most certainly what the at() command is for.  Once you've added them they should show up in atq(), however there are also allow/deny files to block regular users you may need to check. At also happens to be run by cron() so you will want to check /etc/crontab to make sure it's enabled (look for /usr/libexec/atrun).

An alternative would be to make a directory for jobs, then use periodic() for scripts written in there that delete themselves when done, but that's a rather ungraceful solution to do what at was made to do.


----------



## klabacita (Mar 27, 2012)

Just 1 issue Morte:



> Now, I was trying to use at, but the command is not working neither, I had asked again but I still don't had any answer.



  Looks like at is not working on pfsense. x(


----------



## Morte (Mar 27, 2012)

"Not working" is a little vague. I'm not familiar with how much pfsense is customized, but at() may be there but disabled.  Does /usr/libexec/atrun exist?  If it does, then add this to the /etc/crontab (basically what is normally in a generic FreeBSD install):


```
#minute hour    mday    month   wday    who     command
*/5     *       *       *       *       root    /usr/libexec/atrun
```

If not I guess you could try periodic(), or possibly have cron call a script that has all the logic used for rotation and when to do it built in. For instance cron calling a script every 5 minutes, but the script may or may not do anything depending on certain criteria.


----------

