[2025-05-12] Syntax highlighting through CSS and markdown

This commit is contained in:
Andrew Conlin 2025-05-12 12:26:47 +01:00
parent fafa5ee954
commit 9f630196ed
6 changed files with 98 additions and 22 deletions

View file

@ -1,7 +1,7 @@
---
title: Fixing php errors in a Nextcloud docker-compose configuration
categories:
- til
category: til
layout: post
tags:
- php
- nextcloud
@ -15,7 +15,7 @@ I was trying to rescan the files in my Nextcloud server (running on Raspberry Pi
I was trying using the following syntax to call `occ` and scan the files:
```
```bash
sudo -u www-data php /path/to/nextcloud/occ files:scan --all
```
@ -32,7 +32,7 @@ It took me a decent amount of time to diagnose the exact issue, but eventually I
Running `php -m` will print out the list of currently installed PHP modules. I noticed I was missing quite a few of the required modules, but the one that was causing my issue was the missing `pdo_mysql` module.
This can be installed by running:
```
```bash
sudo apt-get install php7.4-mysql
```
**Note: This command will change based on your OS, PHP version and database type**
@ -49,7 +49,7 @@ From first glance, this looks like something wrong in the DNS name resolution. T
Eventually however, after a long and perilous journey over the high seas of Nextcloud forums and StackOverflow, I found [this example](https://techoverflow.net/2020/07/17/how-to-run-nextcloud-php-occ-in-a-docker-compose-configuration/) of running `php occ` in a docker-compose configuration.
This led me to running this command:
```
```bash
docker-compose exec -u www-data nextcloud-app php occ files:scan --all
```
**Note: replace nextcloud-app with the name of your Nextcloud container. Also, this command must be run from the directory of your Nextcloud docker-compose.yml**

View file

@ -1,6 +1,7 @@
---
title: Running a Python script periodically in a Docker container using cron
category: til
layout: post
---
Recently, my partner gave a great idea for utilising my old Kindle: generate a "newspaper" each morning from a bunch of RSS feeds, and email it to the Kindle using "Send-to-Kindle" feature (a blog post about this project is in the works).
@ -10,7 +11,7 @@ I loved this idea, and thought it would be no problem to get a Python script up
## 1. Double check the user
A lot of problems with `cron` come down to user privileges. Each user has their own `crontab`, and then there is the system-wide *root* `crontab`. The first issue I ran into with creating a `cron` job inside a container was that Docker created the crontab as a non-root user. This issue presented itself to me when I tried to run the following command, to list the current cronjobs in the Docker container:
```
```bash
docker-compose exec container-name crontab -l
```
This returned the following output:
@ -18,24 +19,24 @@ This returned the following output:
no crontab for root
```
Now, it is not necessarily a problem to have non-root `cron` jobs, but just make absolutely certain that you are creating the jobs with the user you expect. For me, I wanted to run as `root`, so I added to following line to my docker-compose.yml:
```
```yaml
user: root
```
Now, the `root` user will be used when building your Docker image and the created `crontab` will be where you expect.
## 2. Missing dependencies
When `cron` calls your Python script, you may run into issues with `ModuleNotFoundError` or `ImportError`, where Python cannot find your installed packages. This is because `cron` does not have access to your system environment variables, including the Python path. You can resolve most of these errors with imports by adding the `PYTHONPATH` environment variable to your `crontab`. This should be the path to your `site-packages` folder, something like this:
```
```bash
PYTHONPATH=/usr/bin/local/python3
```
You may also need to add a shebang (`#!`) to your Python script to direct `cron` to the correct version. You can find the Python location with one of the following commands:
```
```bash
which python
which py
which python3
```
*NOTE*: These commands must be performed in your Docker container when it is up and running. In `docker-compose` syntax this would be the following (with the name of your container instead of `container-name`):
```
```bash
docker-compose exec container-name which python3
```
You can then add this to the top of your Python script, as follows:
@ -44,11 +45,11 @@ You can then add this to the top of your Python script, as follows:
```
## 3. Still missing dependencies
Some modules will still run into errors even when the PYTHONPATH variable has been set. In particular, I ran into problems with `reportlab` and `Pillow/PIL`:
```
```python
ImportError: cannot import name '_imaging' from 'PIL'
```
This was solved by adding the system PATH to the `crontab` as well. The system path is included in the default `crontab` that is created when you first run `crontab -e`:
```
```bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
```
Therefore, it is a good idea to include it if you are making a new `crontab` to make sure `cron` can find everything it needs to.
@ -72,7 +73,7 @@ I was able to resolve these by adding `python3-dev`, `wheel` and `Cmake` to my `
I hope this helped you resolve some errors! I've included my Dockerfile, docker-compose.yml and crontab below if you want to set up a similar project or adjust your own files. The full repo is also available [here](https://github.com/andrwcnln/watchman).
Dockerfile:
```
```docker
FROM python:3
COPY . .
@ -90,7 +91,7 @@ RUN crontab crontab
CMD cron -f
```
docker-compose.yml:
```
```yaml
version: "2.4"
services:
@ -105,9 +106,9 @@ services:
dockerfile: Dockerfile
```
crontab:
```
```cron
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PYTHONPATH=/usr/bin/local/python3
15 7 * * * python3 /main.py >> /var/log/cron.log 2>&1
```
```

View file

@ -25,11 +25,11 @@ Please visit https://rvm.io/integration/gnome-terminal/ for an example.
So, we need to be in a login shell. Unfortunately, Alacritty does not have a settings GUI [like gnome-terminal](https://rvm.io/integration/gnome-terminal) and similar, and after thorough investigation I couldn't find a way to set this up through Alacritty. We need a different solution.
Luckily, this is possible through the shell itself. You can open a login shell by running the following command (press Ctrl+D to return to the interactive shell):
```
```bash
bash --login
```
This also works with `zsh`, or Z-shell:
```
```bash
zsh --login
```
Unfortunately for all you fishers out there, I couldn't find a way to to this with `fish`. If you know how, [send me an email!](mailto:andrew@andrewconl.in)