Programmatically start GPSD daemon in linux - linux

I am doing a project read GPS values output from a GPS dongle and need to
programmatically start the gpsd daemon.
i.e. I need to automate the following command;
sudo gpsd /dev/ttyUSB0 -F /var/run/gpsd.sock
I was able to read the coordinates through the code after manually starting the daemon as above. But don't know how to start the daemon through he code.

Since gpsd is a daemon, you can just set the daemon up to be run automatically at startup. How to do this depends on which startup system you have. For example, if you have systemd, you have to write a gpsd.service file, something like this
[Unit]
Description=GPSd daemon service file
[Service]
Type=forking
User=root
Group=dialout
TimeoutStartSec=0
ExecStart=/usr/local/sbin/gpsd /dev/ttyUSB0 -F /var/run/gpsd.sock
[Install]
# Grouping mechanism that let systemd start groups of processes up at the same time
WantedBy=multi-user.target
then install it in /lib/systemd/system and finally using the following commands
$ sudo systemctl enable gpsd
$ sudo systemctl start gpsd
the start command is just to run gpsd as systemd daemon without rebooting your system.

for debian its just
dpkg-reconfigure gps

Related

How to send `SIGINT` over `systemctl` to custom daemon/service

I have a script that I would like to run as service on my Linux machine, let's call it my_script.sh. It is executable and can be run over ./path/to/my_scrit.sh.
I also have script that kills my_script.sh, let's call it kill_my_scprit.sh. The contents of kill_my_script.sh are:
#!/bin/bash
sudo kill -SIGINT $(pgrep my_script)
In essence, this should mimic Ctrl+C for my_script.sh.
This works perfectly fine if I run the scripts from a terminal, i.e. in ttyX I run ./path/to/my_scrit.sh to start it and in ttyY ./path/to/kill_my_scrit.sh to initiate the shut down sequence of my_script.sh.
As mentioned before, the goal is to run this as deamon, so I created /etc/systemd/system/my_script.service
[Unit]
....
[Service]
Type=simple
User=root
WorkingDirectory=/path/to/dir/
ExecStart=/path/to/my_script.sh
ExecStop=/path/to/kill_my_script.sh
Restart=on-failure
[Install]
...
Using sudo systemctl start my_script.service starts the script as expected, but using sudo systemctl stop my_script.service starts the shutdown sequence for my_script.sh, but doesn't let it finish... Am I missing something? The shutdown sequence takes roughly 10sec...

Auto-run python script when system is booted on jetson nano

How to auto-run python script made by me when the system is booted on jetson nano?
Step 1: Create a shell file.
sudo nano /usr/local/bin/startup.sh: Type this on the terminal. A new sh file is created. This file consists of the python file that is to be executed. I gave the name startup.sh. It can be any name XYZ.sh
#! /bin/sh: This is called the shebang. This would execute our script using a Bourne shell. This tells the system that the commands in this file should be fed to the interpreter.
sleep 10: This pauses a script for a certain amount of time. He re we pause it for 10 seconds.
In the next line, we insert the code that we use to run the program on the terminal.
OPENBLAS_CORETYPE=ARMV8 /usr/bin/python3 path/of/the/python/code.py
It looks like this:
#! /bin/sh
sleep 10
OPENBLAS_CORETYPE=ARMV8 /usr/bin/python3 /home/sooraj/Downloads/incabin-monitoring-system-main/netstreamfeb17.py
Now we close the file using Ctrl+X. and save it.
Step 2: Create a service file
sudo nano /etc/systemd/system/startup.service
Things to write inside it.
[Unit]
Description = INTU_IPC start-uo specific script
[Service]
Type= idle
ExecStartPre = /bin/sleep 10
ExecStart = /usr/local/bin/startup.sh
User=sooraj# write your user name here
[Install]
WantedBy = multi-user.target
Now we close the file using Ctrl+X. and save it.
step 3: Give permission.
sudo chmod +x /usr/local/bin/startup.sh
step 4: enable, start and stop
sudo systemctl enable startup.service
sudo systemctl start startup.service
sudo systemctl stop.service
After starting, to view if it works, we can observe it in the terminal by
journalctl -u startup.service -f
If we edit the service file for the next time, we need to reload it before enabling and starting.
sudo systemctl daemon-reload
sudo systemctl enable startup.service
sudo systemctl start startup.service
Additional information.
systemctl is-enabled startup.service #checks if the service file is enabled.
systemctl is-failed startup.service #checks if the service file failed to start.
systemctl is-active startup.service #checks if the service file is active.
sudo systemctl list-unit-files — type=service #display the list of service files.
Try StartupApplications and add your python execution shell command with proper path.
An even better solution will be to use crontab.
crontab -e
Add #reboot python path/to/script so that the script gets executed every time you reboot.
This link might help you.
As an alternative to systemd or crontab, you can try pm2. It's very easy to configure and use. Just follow a quick start guide. Or just test it the following way:
pm2 start app.py
pm2 save
Note that you should initially generate a startup script to make it work on boot.

How to start vlc-nox via ssh in a Linux server running in text mode?

Background
I have a Linux server running in text mode with no X installed. I intend to show video and image using directfb to the monitor (actually a TV). I have installed vlc-nox and it runs as expected if it is invoked in default console (physical keyboard).
Issue
When running it via SSH, no video is displayed, but audio is okay. The error is as below:
directfb vout display error: Cannot create primary surface
fb vout display error: cannot get terminal mode (Inappropriate ioctl for device)
core video output error: video output creation failed
core decoder error: failed to create video output
fbi's way
I think fbi also facing the same issue, as it would raise an error like below:
ioctl VT_GETSTATE: Inappropriate ioctl for device (not a linux console?)
But, fbi provides a solution for this case: -T -vt <arg> start on virtual console <arg>
So, sudo fbi -T 1 /path/to/image/file would display image as expected.
Question: What's the vlc's way?
I finally have a solution for this, so I post it here in case someone has the same question.
My understanding is that vlc needs to run under a real tty, not a pseudo tty. My solution is composed of two parts.
Part 1: Let vlc run as daemon mode.
Create a user for the daemon and assign audio and video privilege.
#useradd -c "VLC daemon" -d / -G audio,video -M -p \! -r -s /bin/false -u 75 -U vlcd
Run vlc at startup using tty1
I have tried this with unit under ubuntu.
[Unit]
Description=VLC server
After=network.target auditd.service
Conflicts=getty#tty1.service
[Service]
ExecStart=/usr/bin/vlc -I rc --rc-host 127.0.0.1:8080
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
User=vlcd
Type=simple
#StandardError=tty
StandardOutput=tty
StandardInput=tty
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Alias=vlc.service
Please take notic of the Conflicts, ExecStart, StandardInput, and StandardOutput parts.
In Conflicts, it will bypass the default getty service, otherwise it should be disable manually by #systemctrl disable getty#tty1.
In ExecStart, do not use the -d switch, which meaning that it would keep running and occupying VT 1, which is activated by Alt-F1. -rc enables the remote connect interface.
In StandardInput/Output, specify the tty as input and output device.
Part 2: Remote talk with the daemon, to let it play the file.
As --rc-host specify a local port, we need to ssh to the server first. Then, use telnet to interact (add, play, pause) with vlc.
telnet 127.0.0.1 8080
add /path/to/video/file

Register daemon controllable by start and stop command in Linux

Many system daemon can be started using start/stop command. I was just curious how start/stop works on Linux system. Say I wrote a daemon executable, how should I configure it so that it can be controlled by start/stop in Linux.
I make a daemon in linux (ArchLinux) few years ago, and it works every day perfectly.
There are 2 ways to do this. Short way and long way:
Short Way:
Create a file in /etc/systemd/system/ called for example mydaemon.service :
/etc/systemd/system/mydaemon.service
[Unit]
Description=This is my first daemon! - Fernando Pucci
After=network.target
[Service]
User=root
WorkingDirectory=/root
Type=oneshotmc
RemainAfterExit=yes
ExecStart=/bin/echo -e "Daemon started"
ExecStop=/bin/echo -e "Daemon Stopped"
[Install]
WantedBy=multi-user.target
This service does nothing but show Daemon Started or Stopped. You can change echoes by the sentences you need.
If you need to run some script, try the Long way:
Long way
Create a file in some directory, like root folder or /usr/lib/systemd/scripts called for example
/root/mydaemon.sh
start() {
<your start sentences here
and here>
}
stop() {
<your stop sentences here
and here>
}
case $1 in
start|stop) "$1" ;;
esac
You must to make it runnable (chmod x)
(And you can execute it with start or stop parameter to test it.)
And as second step, create another file in
/usr/lib/systemd/system/mydaemon.service
[Unit]
Description=Second daemon of Fernando Pucci
After=network.target
[Service]
User=root
WorkingDirectory=/root
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c '/root/mydaemon.sh start'
ExecStart=/bin/echo -e "MyDaemon Started"
ExecStop=/bin/bash -c '/root/mydaemon.sh stop'
ExecStop=/bin/echo -e "MyDaemon Stopped"
[Install]
WantedBy=multi-user.target
Starting and Stopping
systemctl start mydaemon
systemctl stop mydaemon
systemctl status mydaemon
systemctl enable mydaemon
systemctl disable mydaemon
You (and someone) can send me a private msg for help about that.

Prevent stop auditd service in Redhat 7

Curently, i want auditd service run forever and user can not stop this via any commands.
Current my auditd service:
~]# systemctl cat auditd
# /usr/lib/systemd/system/auditd.service
[Unit]
Description=Security Auditing Service
DefaultDependencies=no
After=local-fs.target systemd-tmpfiles-setup.service
Conflicts=shutdown.target
Before=sysinit.target shutdown.target
RefuseManualStop=yes
ConditionKernelCommandLine=!audit=0
[Service]
ExecStart=/sbin/auditd -n
## To not use augenrules, copy this file to /etc/systemd/system/auditd.service
## and comment/delete the next line and uncomment the auditctl line.
## NOTE: augenrules expect any rules to be added to /etc/audit/rules.d/
ExecStartPost=-/sbin/augenrules --load
#ExecStartPost=-/sbin/auditctl -R /etc/audit/audit.rules
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
# /etc/systemd/system/auditd.service.d/override.conf
[Service]
ExecReload=
ExecReload=/bin/kill -HUP $MAINPID ; /sbin/augenrules --load
I can't stop this service from command:
# systemctl stop auditd.service
Failed to stop auditd.service: Operation refused, unit auditd.service may be requested by dependency only.
But when i using service auditd stop command. I can stop this service normally.
# service auditd stop
Stopping logging: [ OK ]
How can i prevent it? Thanks
The administrator (root) will always be able to manually kill the auditd process (which is what the service command does). What systemd is doing here is only to prevent the administrator from doing it via the systemctl interface.
In both cases, unprivileged users can not kill the daemon.
If you want to restrict even what root can do, you will have to use SELinux and customize the policy.
Some actions of service command are not redirected to systemctl but run some specific scripts located in /usr/libexec/initscripts/legacy-actions.
In this case, stop command will call this script:
/usr/libexec/initscripts/legacy-actions/auditd/stop
If you want that, the audited service can't be stopped by service command, you can remove this script, the action "stop" will be redirected to systemctl, which will block it b/c of the parameter "RefuseManualStop=yes".
But this doesn't mean that you can't kill the process of course.

Resources