-1

I am using ubuntu and I have written a python program to clean my download folder and a shell script to execute the program. But I need to run the script manually each time. Is there any way to run the script run automatically when a new file is added to that folder? I know there is inotifywait to monitor the folder but how to make that script runs from the time of reboot with setting corn??

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • Did you read [*Advanced Linux Programming*](https://mentorembedded.github.io/advancedlinuxprogramming/) and [syscalls(2)](https://man7.org/linux/man-pages/man2/syscalls.2.html) and [crontab(1)](https://man7.org/linux/man-pages/man1/crontab.1.html) and [inotify(7)](https://man7.org/linux/man-pages/man7/inotify.7.html) ? Are you allowed to use [inotifywatch(1)](https://man7.org/linux/man-pages/man1/inotifywatch.1.html) ? See also [incrontab(5)](https://linux.die.net/man/5/incrontab) – Basile Starynkevitch Apr 19 '21 at 08:30
  • You could also use [inotify-simple](https://pypi.org/project/inotify-simple/) and look into the [RefPerSys](http://refpersys.org/) project – Basile Starynkevitch Apr 19 '21 at 08:36

2 Answers2

1

I don't quite understand why you run Python script, from a bash script, and that Python script does clean your downloads folder. I think it would be much easier if you run just a bash script that cleans the downloads directly instead. This script would look like this:

#!/bin/bash
/usr/bin/rm -rf /home/<your_username>/Downloads

And save it as somescript.sh. Just remember to replace <your_username> with proper value. As to how schedule running a script, one way to do this is using crontab utility in linux. It allows you to run programs at a certain time. Some examples you may find are here: crontab question, here crontab tutorial and here about crontab. Say, for example we would like to run above script at certain hour. Firstly run:

$ sudo crontab -e

To open crontab editor. Then you need to insert the line below to schedule running somescript at 6 pm everyday:

0 18 * * * /<path_to_the_script>/somescript.sh >/dev/null 2>&1

The entry in crontab has the following form:

minutes hour day month weekday (path to the script)

If you need help to start with specifying time, you might want to look at crontab generator. Also, just make sure that the script has the right privileges i.e. can be executed etc. After you save the crontab file it will be automatically scheduled. In case you want to schedule a task to run once, you might want to look into at commandline tool about at command.

Please note that you should more carefully prepare your question to make sure that everyone understands you. Ideally, provide some examples. This way you have a better chances to receive an answer that would actually help you, so I think it's a good deal. Welcome to the community :)

Mr. 0xCa7
  • 100
  • 7
  • thanks for the brief description.but I would like to run the program at the time when a file placed on the download folder instead of setting a corn to check every single minute or like that is there any way for that – richu sherseen Apr 19 '21 at 07:29
  • You should have a look into that: https://unix.stackexchange.com/questions/24952/script-to-monitor-folder-for-new-files – Mr. 0xCa7 Apr 19 '21 at 09:28
  • Also: https://stackoverflow.com/questions/8699293/how-to-monitor-a-complete-directory-tree-for-changes-in-linux – Mr. 0xCa7 Apr 19 '21 at 09:28
1

In order for a program to run always on a UNIX/Linux system, you might add it to the crontab, using the five asterisks, as mentioned in this serverfault post.

The best way to proceed here is to add a rm command in the crontab, using 2</dev/null at the end, in order for error messages (in case the file does not exist), not to flood your logs.

Dominique
  • 16,450
  • 15
  • 56
  • 112