Linux debian 10 how to make a script run a program at startup and auto input value to this program console - linux

Hello guys i'm learning about linux for myself and i wrote a program sample by python as below with filename as "sample_login" at "/home/user/Desktop/sample_login"
ID = Input("ID: ")
print (ID)
pw = Input("PW: ")
print (pw)
Now i want to make a shell script that could change to this program directory and run my program and input id and pw automatically to program console after reboot then how could i do it?i mean perform command as below
cd /home/user/Desktop
./sample_login
#wait ID:
#input user_id
...
Thank you guys so much

In order to run a script at boot up, you can use systemd service files.
sample.service:
[Unit]
Description=sample service
[Service]
User=johndoe
WorkingDirectory=/path/to/working_dir/
ExecStart=/path/to/working_dir/your_script
Restart=always
[Install]
WantedBy=multi-user.target
You just need to move this file to /etc/systemd/system/. and enable it with systemctl enable sample.service so it starts at bootup. Instead of expecting input, you could accept command line parameters that's a bit easier to handle in automated scripts.

Related

Linux: Triggering desktop notification pop-up when service is executed with systemd

I want to trigger a desktop notification pop-up when a service is executed with systemd on my Linux desktop. The main reason why I am doing this is that I want to learn how to work with systemd timers and services by creating my own scheduled jobs and I would like to pop-up a desktop notification, when a service/job is executed, just to know that something is happening.
I have created a basic example to do that:
notifysystemd.sh:
#!/bin/bash
# Variable to hold path to systemd job logs
SYSTEMD_LOG_DIR='/home/jay/scheduledJobLogs/systemDJobLogs'
SYSTEMD_JOB_NAME='NotifySystemD'
CURRENT_MONTH=$(date '+%b')
# Send notification to desktop
notify-send 'You can automate and schedule anything with systemd today!'
# Write down in the log
CURRENT_TIME=$(date '+%Y-%m-%d-%H:%M')
LOG_RECORD="${CURRENT_TIME} SystemD notification job executed."
# Create a directory for systemd jobs logging, if it doesn't already exist. And don't error if it does exist
mkdir -p $SYSTEMD_LOG_DIR/$SYSTEMD_JOB_NAME
# Write the log record!
echo $LOG_RECORD >> $SYSTEMD_LOG_DIR/$SYSTEMD_JOB_NAME/$CURRENT_MONTH.txt
with this service file:
notifysystemd.service:
[Unit]
Description=A basic service to send a desktop notification using the systemd scheduler
Wants=notifysystemd.timer
[Service]
Type=forking
ExecStart=/home/jay/systemDJobs/notifysystemd.sh
Environment="DISPLAY=:0" "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus" "XAUTHORITY=/home/jay/.Xauthority"
[Install]
WantedBy=default.target
and this timer file:
notifysystemd.timer:
[Unit]
Description=Send a notification three minutes after PC start
RefuseManualStart=false # Allow manual starts
RefuseManualStop=false # Allow manual stops
[Timer]
#Execute job if it missed a run due to machine being off
Persistent=true
OnBootSec=180
#File describing job to execute
Unit=notifysystemd.service
[Install]
WantedBy=timers.target
The service is executed correctly with the correct delay (I can see that in the log created), but I am getting no desktop notification.
I have looked into several questions already asked on this forum:
systemd service not executing notify-send
notify-send command doesn't launch the notification through systemd service
Which suggest specifying environment variables in either the .service file or in the shell script.
I have tried all of them and none led to a notification appearing.
I have done the same with cronie, where was sufficient to specify the DISPLAY and DBUS_SESSION_BUS_ADDRESS environement variables the same way as I did in the notifysystemd.service file.
Lastly, if there is a better way how to achieve the same result, but which revolves around usage of systemd, I am opened to optimal, or more ergonomic solutions.
In systemd instead of notify-send put the output in a file.
Write a bash script and start it on desktop login (so this script can notify-send).
In the script write an endless loop.
Use inotifywait in the loop to monitor the output file for modification.
(This will suspend your script and won't eat cpu)
After inotify, read the content of the file and use notify-send.

how to write a linux service to start a service on computer A after a service in computer B is up and running?(computer A and B are in same network)

in my senario I have an linux service on computer that need to run after a service in computer B(computer A and B are in same network).how can I use system.d to do this job?
There are several solutions to this scenario, the simplest solution is to write a bash script like bellow.
before that you should set ssh-key between 2 linux computer.
at first open a file.
nano /usr/bin/script.sh
and Put the following line in it.
#!/bin/bash
ssh root#computerA 'systemctl start YOURServicName'
now you need to run this bash Script as SystemD Service in computer B.
[Unit]
Description=My Shell Script
[Service]
ExecStart=/usr/bin/script.sh
[Install]
WantedBy=multi-user.target
for more info use this link.
Finally
You need to add this line in your B.service file:
After=A.service
I hope my explanation was useful.

Interactive script on stop systemd service

I need to run script when system shutting down (reboot...) that asks user for contributing some actions:
#!/bin/bash
tty
echo ARE YOU SURE?:
read test
echo $test
[Unit]
Description= Minetest server
[Service]
StandardInput=tty-force
ExecStart= /bin/true
ExecStop=/home/user/test.sh
Type=oneshot
StandardInput=tty
StandardOutput=tty
TTYPath=/dev/tty8
TTYReset=yes
TTYVHangup=yes
RemainAfterExit=true
If I'm executing it as ExecStop, I have output like:
not a tty
INPUT TEST:
and no ask for input
Whats wrong with it?
the ExecStop is called by another script ,not from your console, so there is no tty device attached
and the built-in command read requires input to be a tty device
btw , asking user to contribute via a script is fragile and uncontrollable

Fedora 20 how to run script at the end of startup

I am using Fedora 20. I have a two lines bash script needs to be run at the end of the startup. I want it to be run automatically each time when machine is startup. How can I do this?
I tried "sudo crontab -e" to insert my executable script but it always gave me error teling me the the time is not right and cannot modify the file.
You can create a Systemd unit file in /usr/lib/systemd/system/<service_name>.service. Here is a template:
[Unit]
Description=<description_string>
[Service]
WorkingDirectory=<working_directory>
Type=forking
ExecStart=/bin/bash <absolute_path_to_script>
KillMode=process
[Install]
WantedBy=multi-user.target
Replace anything in the angle brackets with your specific information. The 'WantedBy=multi-user.target' is the magic that tells Systemd to run your script on each start.
On the command line, tell Systemd to enable your service:
systemctl enable <service_name>.service
The next time you reboot your script should be run. Logs will be written to /var/log/messages.
Fedora has some basic documentation on unit files: Systemd unit files
You can append /etc/rc.local it runs just after the system starts up.
You may have to create it if doesn't exist:
Check this answer
Charlie's answer is better but you can still use Tiago's answer.
Just don't forget if you want to use /etc/rc.local way, grant execution permission to this file after editing:
chmod +x /etc/rc.local

How to run last and print my script output during boot with systemd?

I’m trying to configure my host during deployment process and to give an output to the screen of what my configuration script is doing.
In RHEL6 it was easy i was echoing what I want to screen or used dialog to display the output, and only when my script was done i got the login prompt.
( I used rc3.d or rc5.d folder with script name S99.myscript.sh)
In RHEL7 i can’t mimic this process.
rc.local does not display my output during booting and also its not guaranteed it will run last.
I guess I need to create a systemd service file that will run my script.
But how do I output the result to the screen while booting?
And how do I make sure I will not get the log-in prompt before my script ends?
below service example works like a charm :)
[Unit]
Description=ldt_bootscript1.service
After=network.target
Before=getty#tty1.service
[Service]
Type=oneshot
ExecStart=/bin/bash -c "/bin/bash /tmp/ldt_scripts/postinstall/rc.firstboot.qas | /usr/bin/dialog --clear --backtitle \"Linux Deployment\" --title \"tests\" --progressbox 20 70 > /dev/console 2>&1"
ExecStartPre=/usr/bin/echo -e \033%G
ExecReload=/bin/kill -HUP $MAINPID
RemainAfterExit=no
WorkingDirectory=/
Environment=TERM=xterm
[Install]
WantedBy=multi-user.target

Resources