Run shell script before shutdown – linux

before_shutdown.sh

#!/bin/bash
#remove file older than 7 days
find /var/log -name "*.log" -type f -mtime +7

#test
touch "/opt/before_shutdown_tmp_file_$(date +%s)"
  • Create systemd service
echo '[Unit]
Description=before_shutdown

[Service]
ExecStart=/bin/true
Type=oneshot
RemainAfterExit=true
ExecStop=/opt/before_shutdown.sh

[Install]
WantedBy=multi-user.target' > /etc/systemd/system/before_shutdown.service
systemctl daemon-reload

systemctl enable before_shutdown

systemctl start before_shutdown

https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown

python argument parser

consumer.py –server kafka:9092 –consumer_group group1 –topic test-topic1 –messages 100 –sleep 1

import sys
import time
import argparse

#consumer.py --server kafka:9092 --consumer_group group1 --topic test-topic1 --messages 100 --sleep 1

parser = argparse.ArgumentParser(description='Optional app description')
parser.add_argument('--server', type=str,
                    help='kafka server with port eg. kafka:9092.')

parser.add_argument('--consumer_group', type=str,
                    help='consumer_group name.')

parser.add_argument('--topic', type=str,
                    help='kafka topic name')

parser.add_argument('--messages', type=int,
                    help='number of message needs to be produced.')

parser.add_argument('--sleep', type=int,
                    help='sleep between message produced.')

args = parser.parse_args()

print("kafka arguments:")
print(args.kafka_server,args.consumer_group,args.topic,args.messages,args.sleep)

More – https://stackoverflow.com/questions/20063/whats-the-best-way-to-parse-command-line-arguments
https://docs.python.org/3/library/argparse.html