# Help with periodic



## balanga (Jul 31, 2019)

Can anyone help with a daily periodic script to delete any directories in /tmp which are over six months old?

There may well be something similar available... should this be OK (from /etc/periodic.conf.local)? 


```
# 110.clean-tmps
#daily_clean_tmps_enable="NO"<-><------><------><------># Delete stuff daily
daily_clean_tmps_enable="YES"<-><------><------><------># Delete stuff daily
daily_clean_tmps_dirs="/tmp"<--><------><------><------># Delete under here
daily_clean_tmps_days="183"<---><------><------><------># If not accessed for
daily_clean_tmps_ignore=".X*-lock .X11-unix .ICE-unix .font-unix .XIM-unix"
daily_clean_tmps_ignore="$daily_clean_tmps_ignore quota.user quota.group .snap"
daily_clean_tmps_ignore="$daily_clean_tmps_ignore .sujournal"
<------><------><------><------><------><------><------># Don't delete these
daily_clean_tmps_verbose="YES"<><------><------><------># Mention files deleted
```


----------



## olli@ (Jul 31, 2019)

Yeah, sure, that looks OK.  That “`<---->`” stuff is meant to be Tab characters, right?

By the way, I prefer to mount a TMPFS on /tmp, so it is cleaned automatically each time the machine boots. The following entry in /etc/fstab takes care of that:

```
tmpfs   /tmp   tmpfs   rw,mode=01777,size=10G,inodes=262144,nosuid   0   0
```
You might want to adjust the size and inode limits appropriately.


----------



## balanga (Jul 31, 2019)

I actually keep a lot of stuff in /tmp so deleting it automatically would cause problems. 

However my periodic script doesn't seem to work. I created a crontab with
`32     16       *      *      *      root      periodic daily`

/var/log/cron contains:-


> Jul 31 16:32:00 tester /usr/sbin/cron[8717]: (root) CMD (root^I  periodic daily)



which looks like it ran, but I can't tell which script ran and no files were deleted.


----------



## SirDice (Jul 31, 2019)

balanga said:


> I actually keep a lot of stuff in /tmp so deleting it automatically would cause problems.


From hier(7):

```
/tmp/      temporary files that are not guaranteed to persist across
                system reboots
```



balanga said:


> However my periodic script doesn't seem to work. I created a crontab with


Why are you doing this? If you look in /etc/crontab you'll notice periodic(8) is already completely set up. By default.

```
# Perform daily/weekly/monthly maintenance.
1       3       *       *       *       root    periodic daily
15      4       *       *       6       root    periodic weekly
30      5       1       *       *       root    periodic monthly
```

So, maybe you should read periodic.conf(5)?


----------



## balanga (Jul 31, 2019)

I've read  periodic.conf(5) several times and am clearly not understanding it... My /tmp never gets tidied up and I'm trying figure out why.  On one system I have a /etc/defaults/periodic.conf, /etc/periodic with subdirectories for daily monthly security weekly. There is no /etc/periodic.conf although there is on a different system, but have no idea where it came from.  Both systems have a /usr/local/etc/periodic with several subdirectories inside.

I want to test whether periodic will remove all files over x number of days from a given subdirectory  and can't work out how. I created a /etc/periodic.conf.local - a copy of /etc/defaults/periodic.conf with the following amendment:-

```
# 110.clean-tmps
#daily_clean_tmps_enable="NO"<-><------><------><------># Delete stuff daily
daily_clean_tmps_enable="YES"<-><------><------><------># Delete stuff daily
daily_clean_tmps_dirs="/tmp/ports"<----><------><------><------># Delete under here
daily_clean_tmps_days="3"<-----><------><------><------># If not accessed for
daily_clean_tmps_ignore=".X*-lock .X11-unix .ICE-unix .font-unix .XIM-unix"
daily_clean_tmps_ignore="$daily_clean_tmps_ignore quota.user quota.group .snap"
daily_clean_tmps_ignore="$daily_clean_tmps_ignore .sujournal"
<------><------><------><------><------><------><------># Don't delete these
daily_clean_tmps_verbose="YES"<><------><------><------># Mention files deleted
```
I'm trying to delete files I've placed in /tmp/ports but it isn't happening. I can't figure out what I've missed. I have changed /etc/crontab and can see from /var/log/cron that periodic daily has run, but it didn't do what I wanted.


----------



## SirDice (Jul 31, 2019)

balanga said:


> I created a /etc/periodic.conf.local - a copy of /etc/defaults/periodic.conf with the following amendment:-


Don't copy the whole file. Only put the settings you want to change in /etc/periodic.conf. It works similar to /etc/defaults/rc.conf and /etc/rc.conf. The default settings are in the /etc/defaults/*.conf files and your settings go in /etc/*.conf. The /etc/*.conf.local files can be used if you have network-wide settings in /etc/*.conf and local (to that machine) settings go in /etc/*.conf.local. You rarely use the *.local files though. 

All scripts in /etc/periodic/daily/ are executed during the daily periodic. Not all those scripts are enabled. That's what the `<scriptname>_enable` variables do. Some are enabled by default, the daily security and run emails for example. And some, like the 110.clean-tmps script, need to be explicitly enabled. The numbers in front of the script's name indicate the order in which they are executed. The weekly and monthly scripts work exactly the same, but they are obviously only executed on a weekly or monthly basis.



balanga said:


> I'm trying to delete files I've placed in /tmp/ports but it isn't happening.


Look at the timestamp of the files. If you copy files the timestamp will be "now". Your settings show that you want to remove files that are, at least, 3 days old.


----------



## olli@ (Aug 1, 2019)

balanga said:


> I actually keep a lot of stuff in /tmp so deleting it automatically would cause problems.


/tmp is intended for short-lived temporary files that need not persist across reboots (that's why it is typically a TMPFS which can also be a nice performance boost). For temporary files that _need_ to persist, /var/tmp should be used. That's the reason why, for example, vi(1) places its recovery files in /var/tmp.
Of course you can also use your own directory of choice. For example, I sometimes mount a large spare file system on /trash and put things like /usr/obj and /usr/ports into it (via symlinks). It is exempt from backups, of course.


> However my periodic script doesn't seem to work. I created a crontab with
> `32     16       *      *      *      root      periodic daily`


Did you put that line into the system crontab /etc/crontab, or have you created a user crontab (including root user) with the crontab(1) command? I'm asking because the syntax is slightly different. Only in the former case there is a column for the user name (in this case “root” in the sixth column). In the latter case you have to omit the user name, otherwise it would be taken as the first word of the actual command to execute, and there probably is no command “root”, so this would fail. The owner of the crontab should receive an error message via e-mail if that happens.


----------



## balanga (Aug 2, 2019)

olli@ said:


> Did you put that line into the system crontab /etc/crontab, or have you created a user crontab (including root user) with the crontab(1) command? I'm asking because the syntax is slightly different. Only in the former case there is a column for the user name (in this case “root” in the sixth column). In the latter case you have to omit the user name, otherwise it would be taken as the first word of the actual command to execute, and there probably is no command “root”, so this would fail. The owner of the crontab should receive an error message via e-mail if that happens.



At first I created a crontab for root. not knowing any better, but then deleted it, so now cron() only uses /etc/crontab and var/log/cron shows that `periodic daily` is getting launched at the correct time. I have enabled *# 110.clean-tmps * in /etc/defaults/periodic.conf but it doesn't seem to work.  *verbose* is set to yes but am not sure where any output is written, var/log/cron  only show that it has run. Looks like I've missed out a vital step.


----------



## SirDice (Aug 2, 2019)

balanga said:


> I have enabled *# 110.clean-tmps * in /etc/defaults/periodic.conf


Seriously? Haven't you been paying attention?



SirDice said:


> The default settings are in the /etc/defaults/*.conf files and *your settings go in /etc/*.conf*.




```
The file periodic.conf contains a description of how daily, weekly and
     monthly system maintenance jobs should run.  It resides in the
     /etc/defaults directory and parts may be overridden by a file of the same
     name in /etc, which itself may be overridden by the
     /etc/periodic.conf.local file.
```
From periodic.conf(5)



balanga said:


> but am not sure where any output is written


Where does output from cron scripts end up? It's mailed to the account where the cronjob runs on. So, if it's root, then check root's mail.


----------

