Skip to content

Commit ff66ff5

Browse files
authored
fix(docker): Add default crontab download command (#1321)
* docs(docker): Recommended image DEFAULT_WORKSPACE * fix(docker): Add default crontab download command Clarify the `/config/ytdl-sub-configs/cron` script with explanatory comments and a default `--dry-run` command. Also change the wrapper script to echo commands for easier debugging. To update an existing script, move the old script aside, restart the container to regenerate it, and edit the new script. Also clarify the Automating page in the Getting Started guide docs. * fix(docker): Unintentional unattended dry runs [PR feedback](#1321 (comment)) prompted me to reconsider having a default command at all. We should assume, unfortunately, that many new users will just skim the docs enough to enable the image's cron integration but not actually incrementally test their configuration. In those cases, they'd end up sending dry-run non-download requests for all their subscriptions every 6 hours for no good reason. There's just no way to provide a default command that isn't also providing a footgun. * docs(docker): More open cron schedule generator From [PR feedback](#1321 (comment)), this seems like less of an ad than the previous and the source for the page is itself open source. * docs(docker): Document image environment footgun From [PR feedback](#1321 (comment)). * docs(automate): Avoid external link 404 responses * docs(automate): False simultaneous run warning * docs(automate): Clarify Docker daemon restarts * docs(automate): Revert run start non-recommended * docs(automate): Remove footgun manual run command Addressing this underlying issue requires more thought and should be a separate PR. * docs(automate): Restore env var footgun in example
1 parent 64820da commit ff66ff5

8 files changed

Lines changed: 123 additions & 40 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ __preset__:
6868

6969
# Pass any arg directly to yt-dlp's Python API
7070
ytdl_options:
71-
cookiefile: "/config/cookie.txt"
71+
cookiefile: "/config/ytdl-sub-configs/cookie.txt"
7272

7373
###################################################################
7474
# TV Show Presets. Can replace Plex with Plex/Jellyfin/Emby/Kodi

docker/root/custom-cont-init.d/defaults

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ if [ "$CRON_SCHEDULE" != "" ] ; then
4747

4848
# create cron script wrapper
4949
echo '#!/bin/bash' > "$CRON_WRAPPER_SCRIPT"
50+
# Echo commands for easier user debugging:
51+
echo "set -x" >> "$CRON_WRAPPER_SCRIPT"
5052
echo "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" >> "$CRON_WRAPPER_SCRIPT"
5153
echo "cd \"$DEFAULT_WORKSPACE\"" >> "$CRON_WRAPPER_SCRIPT"
5254
echo ". \"$CRON_SCRIPT\" >> \"$LOGS_TO_STDOUT\" 2>&1" >> "$CRON_WRAPPER_SCRIPT"

docker/root/defaults/cron

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
echo "Beginning cron job..."
2-
31
# Place your ytdl-sub command(s) here.
4-
# This script is executed in the same relative path as this file.
2+
#
3+
# This script is executed in the same directory as this file which also contains the
4+
# default `./config.yaml` and `./subscriptions.yaml`, so you don't need to use the
5+
# `--config` CLI option or pass a `SUBPATH` to the `$ ytdl-sub sub` sub-command.
6+
#
7+
# Test your configuration and subscriptions carefully before automating downloads to
8+
# prevent triggering throttles or bans:
9+
#
10+
# https://ytdl-sub.readthedocs.io/en/latest/guides/getting_started/downloading.html
11+
#
12+
# Once you've tested your configuration and you're ready to download entries unattended,
13+
# remove the next line and un-comment the following line:
14+
echo "WARNING: Read /config/ytdl-sub-configs/cron and modify to automate downloads."
15+
# ytdl-sub sub

docker/root/defaults/subscriptions.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ __preset__:
2020

2121
# Pass any arg directly to yt-dlp's Python API
2222
# ytdl_options:
23-
# cookiefile: "/config/cookie.txt"
23+
# cookiefile: "/config/ytdl-sub-configs/cookie.txt"
2424

2525
###################################################################
2626
# Subscriptions nested under this will use the

docs/source/config_reference/subscription_yaml.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ supply a cookies file path.
3131
3232
# Directly set plugin options:
3333
ytdl_options:
34-
cookiefile: "/config/cookie.txt"
34+
cookiefile: "/config/ytdl-sub-configs/cookie.txt"
3535
3636
Layout
3737
------
Lines changed: 96 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,132 @@
1-
Automating Downloads
2-
====================
1+
Automating
2+
==========
33

4-
:ref:`Guide for Docker and Unraid Containers <guides/getting_started/automating_downloads:docker and unraid>`
4+
Automate downloading your subscriptions by running the :ref:`'sub' sub-command
5+
<usage:subscriptions options>` periodically. There are various tools that can run
6+
commands on a schedule you may use any of them that work with your installation
7+
method. Most users use `cron`_ in `Docker containers <docker and unraid_>`_.
58

6-
:ref:`Guide for Linux <guides/getting_started/automating_downloads:linux>`
7-
8-
:ref:`Guide for Windows <guides/getting_started/automating_downloads:windows>`
9-
10-
.. _cron scheduling syntax: https://crontab.guru/#0_*/6_*_*_*
11-
12-
13-
.. _docker-unraid-setup:
149

1510
Docker and Unraid
1611
-----------------
1712

18-
Cron is preconfigured in every ytdl-sub docker container. Enable by adding the following
19-
ENV variables to your docker setup.
13+
:doc:`The 'ytdl-sub' Docker container images <../install/docker>` provide optional cron
14+
support. Enable cron support by setting `a cron schedule`_ in the ``CRON_SCHEDULE``
15+
environment variable:
2016

2117
.. code-block:: yaml
18+
:caption: ./compose.yaml
19+
:emphasize-lines: 4
2220
2321
services:
2422
ytdl-sub:
2523
environment:
26-
- CRON_SCHEDULE="0 */6 * * *"
27-
- CRON_RUN_ON_START=false
24+
CRON_SCHEDULE: "0 */6 * * *"
25+
# WARNING: See "Getting Started" -> "Automating" docs regarding throttles/bans:
26+
# CRON_RUN_ON_START: false
27+
28+
Then recreate the container to apply the change and start it to generate the default
29+
``/config/ytdl-sub-configs/cron`` script. Read the comments in that script and edit as
30+
appropriate.
31+
32+
The container cron wrapper script will write output from the cron job to
33+
``/config/ytdl-sub-configs/.cron.log``. The default image ``ENTRYPOINT`` will ``$ tail
34+
...`` that file so you can monitor the cron job in the container's output and thus also
35+
in the Docker logs.
2836

37+
You may also set the ``CRON_RUN_ON_START`` environment variable to ``true`` to have the
38+
image run your cron script whenever the container starts in addition to the cron
39+
schedule.
2940

30-
- ``CRON_SCHEDULE`` follows the standard `cron scheduling syntax`_. The above value will
31-
run the script once every 6 hours.
32-
- ``CRON_RUN_ON_START`` toggles whether to run your cron script on container start in
33-
addition to the cron schedule.
41+
.. warning::
3442

35-
The cron script will reside in the main directory with the file name ``cron``. Cron
36-
logs should show when viewing the Docker logs.
43+
Using ``CRON_RUN_ON_START`` may cause your cron script to run too often and may
44+
trigger throttles and bans. When enabled, your cron script will run *whenever* the
45+
container starts including when the host reboots, when ``# dockerd`` restarts such as
46+
when upgrading Docker itself, when a new image is pulled, when something applies
47+
Compose changes, etc.. This may result in running ``ytdl-sub`` right before or after
48+
the next cron scheduled run.
3749

3850

3951
.. _linux-setup:
4052

41-
Linux
42-
-----
43-
Must configure crontab manually, like so:
53+
Linux, Mac OS X, BSD, or other UNIX's
54+
-------------------------------------
55+
56+
For installations on systems already running ``# crond``, you can also use cron to run
57+
``ytdl-sub`` periodically. Write a script to run ``ytdl-sub`` in the cron job. Be sure
58+
the script changes to the same directory as your configuration and uses the full path to
59+
``ytdl-sub``:
4460

4561
.. code-block:: shell
62+
:caption: ~/.local/bin/ytdl-sub-cron
63+
:emphasize-lines: 2,3
64+
65+
#!/bin/bash
66+
cd "~/.config/ytdl-sub/"
67+
~/.local/bin/ytdl-sub --dry-run sub -o '--ytdl_options.max_downloads 3' |&
68+
tee -a "~/.local/state/ytdl-sub/.cron.log"
69+
70+
Then tell ``# crond`` when to run the script:
4671

47-
crontab -e
48-
0 */6 * * * /config/run_cron
72+
.. code-block:: console
73+
74+
echo "0 */6 * * * ${HOME}/.local/bin/ytdl-sub-cron" | crontab "-"
75+
76+
Remove the ``--dry-run`` and ``-o ...`` CLI options from your cron script when you've
77+
tested your configuration and you're ready to download entries unattended.
4978

5079

5180
.. _windows-setup:
5281

5382
Windows
5483
-------
5584

56-
To be tested (please contact code owner or join the discord server if you can test this
57-
out for us)
85+
For most Windows users, the best way to run commands periodically is `the Task
86+
Scheduler`_:
87+
88+
.. attention::
5889

59-
.. code-block:: powershell
90+
These instructions are untested. Use at your own risk. If you use them, whether they
91+
work or not, please let us know how it went in `a support post in Discord`_ or `a new
92+
GitHub issue`_.
6093

61-
ytdl-sub.exe --config \path\to\config\config.yaml sub \path\to\config\subscriptions.yaml
94+
#. Open the Task Scheduler app.
95+
96+
#. Click ``Create Basic Task`` at the top of the right sidebar.
97+
98+
#. Set all the fields as appropriate until you get to the ``Action``...
99+
100+
#. For the ``Action``, select ``Start a program``...
101+
102+
#. Click ``Browse...`` to the installed ``ytdl-sub.exe`` executable...
103+
104+
#. Add CLI arguments to ``Add arguments (optional):``, for example ``--dry-run sub -o
105+
'--ytdl_options.max_downloads 3'``...
106+
107+
#. Set ``Start in (optional):`` to the directory containing your configuration.
108+
109+
#. Finish the rest of the ``Create Basic Task`` wizard.
62110

63111

64112
Next Steps
65113
----------
66114

67-
Once you have a significant quantity of subscriptions or have use cases not served using
68-
:doc:`YAML keys and the special characters <./subscriptions>`, it's time to start
69-
:doc:`defining your own custom presets <./first_config>`.
115+
At this point, ``ytdl-sub`` should run periodically and keep your subscriptions current
116+
in your media library without your intervention. As your :doc:`subscriptions file
117+
<./subscriptions>` grows or you discover new use cases, it becomes worth while to
118+
simplify things by :doc:`defining your own custom presets <./first_config>`.
119+
120+
121+
122+
.. _`cron`:
123+
https://en.wikipedia.org/wiki/Cron
124+
.. _`a cron schedule`:
125+
https://crontab.cronhub.io/
126+
127+
.. _`the Task Scheduler`:
128+
https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page
129+
.. _`a support post in Discord`:
130+
https://discord.com/channels/994270357957648404/1084886228266127460
131+
.. _`a new GitHub issue`:
132+
https://github.com/jmbannon/ytdl-sub/issues/new

docs/source/guides/install/docker.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ For example::
3838

3939
$ docker compose run --rm --user="${PUID}:${PGID}" --entrypoint="ytdl-sub" ytdl-sub sub
4040

41+
.. note::
42+
43+
In `the recommended GUI image <gui image_>`_, the ``DEFAULT_WORKSPACE`` directory is
44+
``/config/ytdl-sub-configs/`` which is used throughout the documentation and
45+
examples. In the headless images, that directory is just ``/config/``, so substitute
46+
that path if using a headless image.
47+
4148

4249
Install with Docker Compose
4350
---------------------------

examples/advanced/tv_show_config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ presets:
6969
# ytdl_options lets you pass any arg into yt-dlp's Python API
7070
ytdl_options:
7171
# Set the cookie file
72-
# cookiefile: "/config/youtube_cookies.txt"
72+
# cookiefile: "/config/ytdl-sub-configs/youtube_cookies.txt"
7373

7474
# For YouTube, get English metadata if multiple languages are present
7575
extractor_args:

0 commit comments

Comments
 (0)