prompter: What is the best way to run a bash script periodically on ubuntu?

assistant: There are several ways to run a bash script periodically on Ubuntu, but one of the most common methods is to use the cron utility. Here are the steps to set up a cron job:

1. Open a terminal window.

2. Enter the command crontab -e to edit your user's crontab file.

3. If prompted, select an editor to use with crontab.

4. In the crontab file, add a new line for the job you want to run. The format of a cron job line is as follows:
``` 
*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of the week (0 - 6) (Sunday is 0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of the month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)
``` 
For example, to run a bash script every day at 3 AM, the cron job line would look like this:
``` 
0 3 * * * /path/to/script.sh
``` 
5. Save and exit the crontab file.

Your bash script will now run automatically according to the schedule you specified in the cron job line. You can view your scheduled cron jobs by running the crontab -l command in the terminal.

assistant: you can use the watch command:

`watch -n5 ./script.sh` will execute the script every 5s

or use a cron job:

`*/2 * * * * /home/user/script.sh` will execute the script every 2min

A cron job must be configured with crontab -e, and is more suited for a long running job such as a backup.
Whereas the watch command is easier for simple temporary use cases such as monitoring a transfer or making a quick test.

prompter: How can I execute the script every Thursday on odd days and odd months?

assistant: You can use the cron scheduling syntax to execute a script every Thursday on odd days and odd months. The syntax for cron is as follows:
``` 
*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of the week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of the month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)
``` 
To execute the script every Thursday on odd days and odd months, you can use the following cron expression:
``` 
0 0 * * 4 [ $(( $(date +%d) % 2)) -ne 0 ] && [ $(( $(date +%m) % 2)) -ne 0 ] && /path/to/your/script.sh
``` 
This expression will execute the script at midnight (0:00) on every Thursday (4), and will check whether the current day of the month ($(date +%d)) and month ($(date +%m)) are odd numbers before running the script ([ $(( $(date +%d) % 2)) -ne 0 ] && [ $(( $(date +%m) % 2)) -ne 0 ]). If either the day or the month is even, the script will not be executed. You will need to replace /path/to/your/script.sh with the actual path to your script.

assistant: You can use the cron daemon to schedule the execution of a bash script on Ubuntu. To run a bash script "script.sh" every Thursday on odd days and odd months, follow these steps:

Open your terminal and type the following command to open the crontab file:
    >crontab -e
This will open the crontab file in the default text editor. Add the following line to the end of the file:
    >0 0 * * 4 [ $(( $(date +\%d) \% 2)) -eq 1 ] && [ $(( $(date +\%m) \% 2)) -eq 1 ] && /path/to/script.sh
``Replace `/path/to/script.sh` with the actual path to your bash script.``

Save the file and exit the editor.
This cron job will run the script every Thursday at midnight (0 0 * * 4), and will only execute the script if the day of the month and the month are odd numbers.

Note: Make sure the script has executable permission (use chmod +x script.sh to grant executable permission) and that the path to the script is correct.

prompter: Is there a way to execute a batch script with increasing frequency, until it runs every day for a week, and then go back to low frequency like once a week? So, week 1 and 2, run once a week on monday; week 3 and 4 run twice a week on Monday and Thursday; week 5 and 6, thrice a week on Monday, Wednesday, and Friday; etc.

assistant: The standard way to run a bash script or other command periodically on linux operating systems is using cron. First, run the following command:
```bash
$ crontab -e
```
This will open the crontab file for the current user in your system's default command line editor. This file specifies what commands to run and when to run them. To add your script to your crontab, add a line in the following format:
```crontab
<minute> <hour> <day of month> <month> <day of week> /path/to/script
```
Each of the fields above represents the time at which the command should run as a number, starting from 0. To run the command for all of one of these intervals, use `*` in place of a number. You can additionally run a command every `n` of a particular interval by writing `*/n` for that field.

prompter: Thanks! Is there a way to schedule irregular intervals with crontab? Like running something every three days? Or skipping months? Running once a week one month and then twice a week the next month?

If that can be done with crontab, please write the line or lines that are needed. If that has to be done with a script that gets called by crontab, please write both the crontab line(s) and the script.

assistant: Sure! Here's a an example crontab line that runs a command every 3 days at midnight:
```
0 0 */3 * * <command>
```

Here's another example runs at noon on the first day of each month, skipping April through August.
```
# '-' specifies an inlcusive range of times, while ',' specifies joining two ranges or numbers
0 12 0 0-2,9-11 * <command>

# Can also be written as
0 12 0 jan-mar,sep-dec * <command>
```

A simple way to run a command once a week one month and twice a week another month is to add two entries for a command:
```
# Run at noon on Sunday in January...
0 12 * jan sun <command>

# ... and at noon on Sunday and Wednesday in February
0 12 * feb sun,wed <command>
```

assistant: Yes, you can schedule irregular intervals with crontab by using some special characters. Here are some examples:

1. Running a command every three days:
To run a command every three days, you can use the modulo operator in the day of the month field. For example, the following line will run the command every day that is a multiple of 3:
``` 
0 0 */3 * * /path/to/command
``` 

2. Skipping months:
To skip a month, you can use a comma-separated list of month values in the month field. For example, the following line will run the command every day in January, March, May, July, September, and November:
``` 
0 0 * 1,3,5,7,9,11 * /path/to/command
``` 

3. Running a command once a week one month and twice a week the next month:
To run a command once a week one month and twice a week the next month, you will need to use a combination of the day of the week and day of the month fields. For example, the following line will run the command every Monday in January and every Monday and Wednesday in February:
``` 
0 0 * 1 * [ $(date +\%W) -eq 0 ] && /path/to/command
0 0 * 2 * [ $(date +\%W) -eq 0 -o $(date +\%W) -eq 3 ] && /path/to/command
``` 
Note that the above lines include a shell condition to determine whether to run the command on a particular day. The $(date +\%W) expression returns the current week number, which can be used to determine whether the current day is a Monday (since Mondays always start a new week). The -eq operator is used to compare the week number to the desired day of the week (0 for Monday). The -o operator is used to combine multiple conditions with an OR operator.

If you need to run a more complex scheduling pattern, you may want to consider writing a script that can handle the scheduling logic and then calling that script from crontab. The script can use any programming language that is installed on your system (e.g., Python, Ruby, Perl, etc.).