Ubuntu: How to use Incron and date?



Question:

I have the following code:

/var/www/html/wp-content/uploads/2015/09 IN_CREATE,IN_CLOSE_WRITE /opt/imageopt$  

how can i set the "/2015/09" part to todays date?

Thanks,


Solution:1

Since replacing /2015/09 with "$(/bin/date +/%Y/%m)" in your incron table entry breaks, create a system incron table (in /etc/incron.d/) and manipulate it directly with a root cron job (be generally careful with that). I.e. modify an incron system table entry automatically on a monthly basis to reflect the new date value /YYYY/MM.

$ sudo [yr-txt-editor] incron-daily-tab-update  #!/bin/bash  # script: incron-daily-tab-update  --  owned by root  currentpath=/var/www/html/wp-content/uploads/"$(/bin/date +%Y/%m)"  /usr/bin/printf '%s' "$currentpath IN_CREATE,IN_CLOSE_WRITE /opt/imageopt$" \       > /etc/incron.d/imageopt.incron-rule  exit 0  

Followed by:

$ sudo chmod ug+x incron-daily-tab-update  

Then you have two possibilities:

1/ Move the script to /usr/local/sbin/incron-daily-tab-update and create a cron job to be run by root on the first of each month or ASAP afterwards at boot.

$ sudo crontab -e  @monthly /bin/bash /usr/local/sbin/incron-daily-tab-update  

2/ move the script incron-daily-tab-update to /etc/cron.monthly/. The result will be the same as creating a crontab entry.

Either way add root on a line of its own to /etc/incron.allow; create that file if it does not exist on yr system.

From incrontab(5), "incrontab files are read after any change" so the change in path should be immediately detected by the incrond daemon and should have an immediate effect on the first boot of any given month...

Tell us how this goes.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »