How could I run a shell script with delay - bash

I basically want to run a script which is a server but with 10 second delay, it is because I need some stuff to run before this script.
The server is located in the folder /etc/init.d but basically to make it work I go to that path using the command line and I have to restart the server typing:
sudo ./znodejs.sh stop
And then I start the server again:
sudo ./znodejs.sh start
I would like to know if there is any way to run those commands with a delay.

In order to make a script run on startup first make it executable:
$ sudo chmod 755 /etc/init.d/znodejs.sh
Then you can register the script to be run at startup:
$ sudo update-rc.d znodejs.sh defaults
(Edit)
original answer:
the sleep command sill pause for a given number of seconds:
sudo ./znodejs.sh stop
sleep 10
sudo ./znodejs.sh start

The standard unix command for sleeping is called
sleep
to wait a second, use
sleep 1

Related

Auto Start Script

So I am making a script that can run these commands whenever a server boot/reboot:
sudo bash
su - erp
cd frappe-bench/
bench start >/tmp/bench_log &
I found guides here and there about how can I change user in script I came out with the following script:
#! /bin/sh
sudo -u erp bash
cd /home/erp/frappe-bench/
bench start >/tmp/bench_log &
And, I have created a service at /etc/systemd/system/ and set it to run automatically when the server boots up.
The problem is, whenever I run sudo systemctl start erpnextd.service and checked the status, it came up with this
May 24 17:10:05 appbsystem2 systemd[1]: Started ERPNext | Auto Restart.
May 24 17:10:05 appbsystem2 sudo[18814]: root : TTY=unknown ; PWD=/ ; USER=>erp ; COMMAND=/bin/bash
May 24 17:10:05 appbsystem2 systemd[1]: erpnextd.service: Succeeded.
But it still doesn't start up ERPNext.
All I wanted to do is make a script that will start erpnext automatically everytime a server reboot.
Note: I only install frappe-bench on user erp only
Because you are using systemd, you already have all the features from your script available, and better. So you don't even need the script anymore:
[Unit]
Description=...
[Service]
# Run as user erp.
User=erp
# You probably also want to run as group erp, if it exists.
Group=erp
# Change to this directory before executing.
WorkingDirectory=/home/erp/frappe-bench
# Redirect standard output to the given log file.
StandardOutput=file:/tmp/bench_log
# Redirect standard error to the same log file.
StandardError=file:/tmp/bench_log
# Command line for starting the program. Make sure to use an absolute path!
ExecStart=/full/path/to/bench start
[Install]
WantedBy=multi-user.target
Using crontab (the script will start after every reboot/startup)
#crontab -e
#reboot sh /full/path/to/bench start >/tmp/bench_log
The answer provide by Thomas is very helpful.
However, I found another workaround by adding the path of my script file into the bottom of /etc/rc.local file.
Both method works, just a matter of preference ;)

Run shutdown command inside bash script

I'm trying to make an executable file (bash script) to show me a notification and shutdown my computer when a process is not found.
I will run the script as a Startup Application and I'm using the notify-send and shutdown commands in this script.
The problem is:
(1) If I add myfolder/myscript to the Startup Applications list it can't run the shutdown command (root password is required for this)
(2) If I add the script sudo myfolder/myscript it can't show the notifications via notify-send application.
I've already done a lot of searching around the internet and tried these steps:
(1) Added the script path or /sbin/shutdown to the sudores via sudo visudo
(2) Added su - $USER -c "DISPLAY=$DISPLAY DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus before notify-send command (I found some users reporting that root can't send notifications)`
So... none of them worked. What I'm missing?
What can be done to display notifications AND shutdown?
Here is my code:
#!/bin/bash
#Search for a specific process and sleep if it is found (removed for space saving)
shut_time=$(date --date='10 minutes' +"%T")
notify-send -t 600000 "WARNING:
Program is not running.
Shutting down in 10 minutes (scheduled for $shut_time)."
#ALREADY TESTED BELLOW LINES (DON'T WORK)
#su - $USER -c "DISPLAY=$DISPLAY DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus notify-send -t 600000 'WARNING:
#Program is not running.
#Shutting down in 10 minutes.'"
sudo /sbin/shutdown -h +10 #Tried with our without sudo
I'm running MX Linux 18 (xfce, Debian based).
To execute a terminal or any commands even another bash script within a BASH SCRIPT, all you have to do is simply start with a dollar sign and enclose the whole commands and arguments if any with PARENTHESES as follows.
$(COMMANDS)
In his case, it would be
$(sudo shutdown 10)
The statement above will EXECUTE the SHUTDOWN command for 10 minutes system shutdown and spit out the actual date and time the system will be automatically shutdown just like you would run this command in a console. There is no need to turn the user into sudoer or superuser. Whenever he runs his bash script as a ROOT USER or using SUDO, he will be PROMPTED to enter the root password. That's all he has to do and the above command will be executed.
Plus, if there is ever a need to capture the output of any command or script, do as follow.
my_shuttime=$(sudo shutdown 10)
I think it lacks an entry for shutdown in the sudoers. Please create a file sudo under /etc/sudoers.d and make the following entry:
[YOURUSER] ALL = (ALL) NOPASSWD: /sbin/shutdown
Replace [YOURUSER] with your user account!

How can I execute both commands in an sh file?

I have created an sh file:
#!/bin/bash
sudo python3 /home/pi/Desktop/try.py
sudo matchbox-keyboard
and when I execute the file, the program: try.py runs but the matchbox keyboard does not appear. Only when I close the program will the matchbox keyboard will appear. I want them to both appear at the same time.
The script executes both commands sequentially and waits each time until the command returns.
You could change it to:
#!/bin/bash
sudo python3 /home/pi/Desktop/try.py &
sudo matchbox-keyboard

Bash dropping out of sudo in a script

I need to execute an install script using sudo, but towards the end of the script, the script needs to drop out of sudo and continue as the regular user.
Example:
sudo ./install.sh
script runs and does what it needs to as root
su myscriptuser
service myscript start
Basically, the service myscript start needs to be run by the regular user, not by root.
su myscriptuser starts another shell in the name of myscriptuser and waits until it exits. Then it proceeds to run service myscript start in the name of root again.
What you need instead of the last 2 commands is sudo:
sudo -u myscriptuser service myscript start

bash reboot command not found

I am trying to execute a script on server. When I do crontab -l I get #reboot cd my_project_path; ./start.sh on terminal.
However when I do #reboot cd my_project_path; ./start.sh I get -bash: #reboot: command not found. How do I include reboot command in bash? Sorry if this is a very basic question, but I am not master in bash.
There isn't a command #reboot. I think you're looking for
shutdown -r now
or (possibly)
/sbin/reboot
which will reboot your machine. However, in crontab a #reboot is a scheduled time, so that's the command it would run when your system has just rebooted... so perhaps you really just wanted
cd my_project_path; ./start.sh
#reboot in crontab means "Do that / execute that on server boot"

Resources