Skip to content

Other Server Modifications

Ilias Koukovinis edited this page Sep 28, 2025 · 5 revisions

Configuring User Login Expiration

If you want to set an expiration date for users who have been logged in, follow the steps below: First, ensure that cron is installed on your server:

sudo apt install cron

Then, create a new script called delete_expired_logins.sh inside a folder (e.g., server-scripts or within database configurations). Here's the content of the script:

#!/bin/bash

# Variables for PostgreSQL credentials
PG_USER="ermis_admin"
PG_PASSWORD="ermis_password"
PG_HOST="localhost"
PG_PORT="5432"
PG_DB="ermis_database"

# SQL query to delete rows from table `user_ips` older than one month
SQL_QUERY="DELETE FROM user_devices WHERE logged_in_at < NOW() - INTERVAL '1 month';"

# Run the query using psql
export PGPASSWORD=$PG_PASSWORD
psql -U $PG_USER -d $PG_DB -h $PG_HOST -p $PG_PORT -c "$SQL_QUERY"

After creating the script, you need to make it executable. Run the following command:

chmod +x /path/to/server-scripts/delete_expired_logins.sh

Now, schedule the script to run automatically using cron. To run the script every day at midnight, open the crontab for editing:

crontab -e

Then, add the following line to schedule the script:

0 0 * * * /path/to/server-scripts/delete_expired_logins.sh

This cron job will execute the script every day at midnight, which will, in turn, run the PostgreSQL query to delete rows from the user_ips table where the logged_in_at timestamp is older than one month.

To verify that the cron job has been added, you can list the current cron jobs by running:

crontab -l

You should see the job listed, like this:

0 0 * * * /path/to/server-scripts/delete_expired_logins.sh

Which ensures the cron job is set up and will run as expected.

Clone this wiki locally