@@ -0,0 +1,88 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "70914fb0-e3d4-4233-ad33-70a1a2517a6b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "b04d189c-260b-427d-af65-9ad88ac5ecf2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"['.bash_logout', '.bash_profile', '.bashrc', '.zshrc', '.config', '.Xclients', '.dir_colors', '.profile', '.zshenv', '.local', 'Schreibtisch', 'Downloads', 'Vorlagen', 'Öffentlich', 'Dokumente', 'Musik', 'Bilder', 'Videos', '.cache', '.mozilla', '.lyrics', '.gemini', 'my_sway_konfig', 'Projekte', '.ansible', '.ssh', '.vim', '.tmux.conf', '.npm', 'create_lxc', 'lxc_folder', '.docker', 'ebook', '.bash_history', '.zhistory', 'Nextcloud', '.yarn', '.pki', 'sonovum', '.wget-hsts', '.joplin', 'JoplinBackup', '.python_history', '.gnupg', '.gitconfig', '.gitignore', '.env', '.ollama', '.claude', '.steam', '.pulse-cookie', '.icons', 'cert', 'podcast-studio', 'scripts', 'Screenshots', '.claude.json.backup.1772740061126', '.claude.json.backup.1772740061140', '.claude.json.backup.1772740137318', '.claude.json.backup.1772740278244', '.claude.json.backup.1772740278252', '.claude.json', 'Media-Data', '.smbcredentials-media', 'nfs', '.mplayer', '.steampath', '.steampid', '.eclipse', '.swt', '.java', 'conf.ini', 'token_vaultwarden', 'jenkins_status_example', '.sigstore', 'tinydb_vs_sqlite.md', '.cargo', '.viminfo', '.ipython', '.jupyter']\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(os.listdir(\"/home/jonnybravo/\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "55709fef-1402-4c9f-b2ee-2be8e55a137c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"['ver_next_einbinden.md', 'yubikey_setup_guide.md', 'networkconfig_dns.md', 'blueray_install_manjaro.md', 'calcurse_tastenkürzel.md', 'Stretchen_laufen.md']\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"all_files_with_md = [ x for x in os.listdir(\"/home/jonnybravo/Dokumente\") if x.endswith(\".md\") ]\n",
|
||||
"print(all_files_with_md)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "89b5f549-36c9-4a62-83ba-69ad84ec6be6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "692cc39e-ddf8-4bcb-ad08-baed47e37a74",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.14.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#! /usr/bin/env Python3
|
||||
|
||||
import os
|
||||
|
||||
class create_unit_service():
|
||||
def __init__(self, UnitName = str,systemd_sysfolder = "/etc/systemd/system", execstart = str, systemd_type = "simple") -> None:
|
||||
self.UnitName = UnitName
|
||||
self.systemd_systemfolder = systemd_sysfolder
|
||||
self.execstart = execstart
|
||||
if systemd_type == "simple" or systemd_type == "oneshot":
|
||||
self.systemd_type = systemd_type
|
||||
else:
|
||||
print("Kein richtiger Typ")
|
||||
raise "systemd_type ist nicht richtig"
|
||||
|
||||
def create_unit(self):
|
||||
UnitFile = self.systemd_systemfolder + os.sep + self.UnitName + ".service"
|
||||
if os.path.exists(self.systemd_systemfolder):
|
||||
if os.path.exists(UnitFile):
|
||||
print("Gibt es bereits")
|
||||
else:
|
||||
print("Wird angelegt !!")
|
||||
with open(UnitFile, "w") as ufile:
|
||||
print("[Unit]",
|
||||
"[Service]",
|
||||
"Type=oneshot",
|
||||
"PrivateTmp=yes",
|
||||
"DynamicUser=yes",
|
||||
"ExecStart={my_execStart}".format(my_execStart=self.execstart), file=ufile, sep="\n")
|
||||
os.popen("systemctl daemon-reload")
|
||||
|
||||
def delete_unit(self):
|
||||
pass
|
||||
|
||||
if __name__ == "__main__":
|
||||
new_unit = create_unit_service(UnitName="TestUnit",
|
||||
execstart="""/usr/bin/echo "Ich bin eine TestUnit %p" """,
|
||||
systemd_type="oneshot")
|
||||
new_unit.create_unit()
|
||||
@@ -0,0 +1,14 @@
|
||||
import os
|
||||
|
||||
|
||||
print(os.listdir("/home/jonnybravo/"))
|
||||
|
||||
|
||||
all_files_with_md = [ x for x in os.listdir("/home/jonnybravo/Dokumente") if x.endswith(".md") ]
|
||||
print(all_files_with_md)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "70914fb0-e3d4-4233-ad33-70a1a2517a6b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "b04d189c-260b-427d-af65-9ad88ac5ecf2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"['.bash_logout', '.bash_profile', '.bashrc', '.zshrc', '.config', '.Xclients', '.dir_colors', '.profile', '.zshenv', '.local', 'Schreibtisch', 'Downloads', 'Vorlagen', 'Öffentlich', 'Dokumente', 'Musik', 'Bilder', 'Videos', '.cache', '.mozilla', '.lyrics', '.gemini', 'my_sway_konfig', 'Projekte', '.ansible', '.ssh', '.vim', '.tmux.conf', '.npm', 'create_lxc', 'lxc_folder', '.docker', 'ebook', '.bash_history', '.zhistory', 'Nextcloud', '.yarn', '.pki', 'sonovum', '.wget-hsts', '.joplin', 'JoplinBackup', '.python_history', '.gnupg', '.gitconfig', '.gitignore', '.env', '.ollama', '.claude', '.steam', '.pulse-cookie', '.icons', 'cert', 'podcast-studio', 'scripts', 'Screenshots', '.claude.json.backup.1772740061126', '.claude.json.backup.1772740061140', '.claude.json.backup.1772740137318', '.claude.json.backup.1772740278244', '.claude.json.backup.1772740278252', '.claude.json', 'Media-Data', '.smbcredentials-media', 'nfs', '.mplayer', '.steampath', '.steampid', '.eclipse', '.swt', '.java', 'conf.ini', 'token_vaultwarden', 'jenkins_status_example', '.sigstore', 'tinydb_vs_sqlite.md', '.cargo', '.viminfo', '.ipython', '.jupyter']\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(os.listdir(\"/home/jonnybravo/\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "55709fef-1402-4c9f-b2ee-2be8e55a137c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"['ver_next_einbinden.md', 'yubikey_setup_guide.md', 'networkconfig_dns.md', 'blueray_install_manjaro.md', 'calcurse_tastenkürzel.md', 'Stretchen_laufen.md']\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"all_files_with_md = [ x for x in os.listdir(\"/home/jonnybravo/Dokumente\") if x.endswith(\".md\") ]\n",
|
||||
"print(all_files_with_md)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "89b5f549-36c9-4a62-83ba-69ad84ec6be6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "692cc39e-ddf8-4bcb-ad08-baed47e37a74",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.14.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
+55
-15
@@ -1,12 +1,34 @@
|
||||
#! /usr/bin/env Python3
|
||||
|
||||
import os
|
||||
import os
|
||||
from posix import stat
|
||||
from jinja2 import Template
|
||||
import subprocess
|
||||
|
||||
|
||||
def is_root():
|
||||
if os.geteuid() == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
class create_unit_service():
|
||||
def __init__(self, UnitName = str,systemd_sysfolder = "/etc/systemd/system", execstart = str, systemd_type = "simple") -> None:
|
||||
def __init__(self,
|
||||
UnitName: str,
|
||||
systemd_sysfolder: str = "/etc/systemd/system",
|
||||
UnitExecStart = str,
|
||||
systemd_type: str = "simple",
|
||||
UnitDescription: str = "Meine Unit",
|
||||
UnitUser = str,
|
||||
UnitWorkDir: str = "/home/jonnybravo/"
|
||||
) -> None:
|
||||
|
||||
self.UnitName = UnitName
|
||||
self.systemd_systemfolder = systemd_sysfolder
|
||||
self.execstart = execstart
|
||||
self.UnitExecStart = UnitExecStart
|
||||
self.UnitDescription = UnitDescription
|
||||
self.UnitWorkDir = UnitWorkDir
|
||||
self.UnitUser = UnitUser
|
||||
if systemd_type == "simple" or systemd_type == "oneshot":
|
||||
self.systemd_type = systemd_type
|
||||
else:
|
||||
@@ -20,20 +42,38 @@ class create_unit_service():
|
||||
print("Gibt es bereits")
|
||||
else:
|
||||
print("Wird angelegt !!")
|
||||
with open(UnitFile, "w") as ufile:
|
||||
print("[Unit]",
|
||||
"[Service]",
|
||||
"Type=oneshot",
|
||||
"PrivateTmp=yes",
|
||||
"DynamicUser=yes",
|
||||
"ExecStart={my_execStart}".format(my_execStart=self.execstart), file=ufile, sep="\n")
|
||||
os.popen("systemctl daemon-reload")
|
||||
with open('template/LinuxUnit.j2') as f:
|
||||
tmpl = Template(f.read())
|
||||
with open(UnitFile, "w") as uf:
|
||||
print(tmpl.render(
|
||||
description = self.UnitDescription,
|
||||
working_dir = self.UnitWorkDir,
|
||||
exec_command = self.UnitExecStart,
|
||||
user = self.UnitUser,
|
||||
env_vars = {"PYTHONPATH": "."},
|
||||
service_type = self.systemd_type
|
||||
),file=uf)
|
||||
|
||||
def unit_start_and_load(self):
|
||||
UnitFile = self.systemd_systemfolder + os.sep + self.UnitName + ".service"
|
||||
status_daemon_reload = subprocess.run(["systemctl", "daemon-reload"])
|
||||
if status_daemon_reload.returncode != 0:
|
||||
raise Exception( f'Systemd reload Invalid result: { status_daemon_reload.returncode }' )
|
||||
else:
|
||||
print(f"{self.UnitName} wurde angelegt und kann genutzt werden !!! Ein starten oder Enable wurde bewusst nicht eingebaut.")
|
||||
|
||||
def delete_unit(self):
|
||||
pass
|
||||
|
||||
if __name__ == "__main__":
|
||||
new_unit = create_unit_service(UnitName="TestUnit",
|
||||
execstart="""/usr/bin/echo "Ich bin eine TestUnit %p" """,
|
||||
systemd_type="oneshot")
|
||||
new_unit.create_unit()
|
||||
if is_root():
|
||||
new_unit = create_unit_service(UnitName="TestUnit",
|
||||
UnitExecStart="""/usr/bin/echo "Ich bin eine TestUnit %p" """,
|
||||
UnitUser="jonnybravo",
|
||||
systemd_type="oneshot")
|
||||
new_unit.create_unit()
|
||||
new_unit.unit_start_and_load()
|
||||
else:
|
||||
print("Du bist nicht root führe das Skript mit sudo aus")
|
||||
print('sudo python <skript>')
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
- systemd reload
|
||||
- systemd start
|
||||
- systemd enable
|
||||
@@ -0,0 +1,40 @@
|
||||
[Unit]
|
||||
Description={{ description }}
|
||||
# Stellt sicher, dass das Netzwerk bereit ist
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
# 'simple' ist der Standard, 'exec' wartet, bis der Prozess wirklich gestartet ist
|
||||
Type={{ service_type }}
|
||||
|
||||
# Arbeitsverzeichnis (wichtig für relative Pfade im Skript)
|
||||
WorkingDirectory={{ working_dir }}
|
||||
|
||||
# Ausführen als spezifischer Benutzer (Sicherheit!)
|
||||
User={{ user }}
|
||||
Group={{ user }}
|
||||
|
||||
# Umgebungsvariablen (z.B. für Python Pfade oder API Keys)
|
||||
{% if env_vars -%}
|
||||
{% for key, value in env_vars.items() -%}
|
||||
Environment="{{ key }}={{ value }}"
|
||||
{% endfor -%}
|
||||
{% endif %}
|
||||
|
||||
# Vor dem Start kurz warten (optional, falls nötig)
|
||||
{% if wait_before_start -%}
|
||||
ExecStartPre=/usr/bin/sleep {{ wait_before_start }}
|
||||
{% endif %}
|
||||
|
||||
# Der eigentliche Befehl
|
||||
ExecStart={{ exec_command }}
|
||||
|
||||
# Neustart-Logik: Startet den Dienst automatisch neu, wenn er abstürzt
|
||||
Restart=on-failure
|
||||
# Wartezeit zwischen Neustartversuchen
|
||||
RestartSec=5s
|
||||
|
||||
[Install]
|
||||
# Definiert, wann die Unit beim Booten gestartet wird
|
||||
WantedBy=multi-user.target
|
||||
Reference in New Issue
Block a user