4

I have created batch file which starting & stopping celery services.

I want to implement code in batch file so that files older than week will be deleted.

celery_restart.bat

NET STOP "CeleryBeat"
NET START "CeleryBeat"
NET STOP "Celery2"
NET START "Celery2"
Moon
  • 4,014
  • 3
  • 30
  • 66
  • 2
    What have you tried? Have you considered using `logrotate`? Why did you use the `python` tag? – norok2 Sep 25 '19 at 06:10
  • 1
    As I am using windows machine so not able to use logrotate so I have created batch file using which I am restarting celery services & able to rotate log file along with also. Celery create new log file every time I am restarting celery services using batch file. – Moon Sep 25 '19 at 06:15

2 Answers2

2

For Windows:

ForFiles /p "your\folder\with\logs" /m *.log /d -7 /c "cmd /c del @file"

Add /s to delete log files in subfolders as well. If you want to delete all files in the folder older than 7 days, just remove the /m *.log option.

Compo
  • 36,585
  • 5
  • 27
  • 39
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • 1
    above code working. I also want to delete .pid files alongwith log files so is there any way to delete .pid files also with same line because currently I have used two lines one for .log & another for .pid. – Moon Sep 25 '19 at 06:29
  • I recommend running `ForFiles /?` from the command line - it's pretty self-explanatory (literally). However to do what you want, there's no easy option - that question is answered here: https://stackoverflow.com/questions/11719347/using-forfiles-with-multiple-file-types-for-search-mask (I would prefer the first solution given). – Grismar Sep 25 '19 at 20:53
0

Or you can just run some for loops:

@echo off
set comms="CeleryBeat" "Celery2"
for %%i in (*.pid *.log do del %%i /Y
for %%a in (%comms%) do (
    net stop %%a && net start %%a
)

the && operator here will start the service only if the previous command exited with errorlevel 0

Gerhard
  • 22,678
  • 7
  • 27
  • 43