linux run script at startup centos - node.js

i have nodejs App i want
run script at startup centos
my script.sh is
#!/bin/bash
cd /home/mb/Desktop/Voip
gulp
my Executing Commands and Scripts at Reboot & Startup in Centos 7 is
crontab -e
#reboot sh /home/mb/Desktop/script.sh
does not work

The simplest way is to edit /etc/rc.local :
/bin/sh /home/mb/Desktop/script.sh &
or try to debug your cron :
crontab -e
#reboot /bin/sh /home/mb/Desktop/script.sh &>/tmp/debug
Then, cat /tmp/debug

Related

When I try execute a shell script from within a shell script via a `#reboot` cron it does not run correctly, only works correctly executing in CML

When I try execute a shell script from within a shell script it works when executing in terminal manually. However, when executing it via a #reboot cron via sudo crontab -e on Raspberry Pi OS it runs everything apart from sh /home/pi/script.sh within the shell script.
My shell script:
#!/bin/sh
clear
sleep 5
python /home/pi/Desktop/Relay-Script-On.py
sleep 3
sh /home/pi/script.sh
sleep 5
python /home/pi/Desktop/Relay-Script-Off.py
sleep 3
I have made the other shell file executable using sudo chmod +x
Note I am still new to shell (apologies if there is an obvious error here).

Autorun C++ exe with crontab on boot

I have created crontab file using the following command.
crontab -e
Then in crontab file
#reboot sleep 120 && /home/xavier/run.sh
run.sh file has
#crontab -e
SHELL=/bin/bash
PATH=/bin:/sbin/:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
cd /opt/nvidia/deepstream/deepstream-6.0/sources/apps/sample_apps/deepstream-app
./deepstream-app -c ../../../../samples/configs/deepstream-app/stluke_1080p_dec_infer-resnet_tracker_sgie_tiled_display_int8_record.txt
deepstream-app program runs after two minutes. But it stops. I can see in systemmonitor.
I'm not sure passing arguments to deepstream-app is correct.
On terminal, the following two commands are used to run c++ exe file.
cd /opt/nvidia/deepstream/deepstream-6.0/sources/apps/sample_apps/deepstream-app
./deepstream-app -c ../../../../samples/configs/deepstream-app/stluke_1080p_dec_infer-resnet_tracker_sgie_tiled_display_int8_record.txt
So I run exactly in shell file.
What could be wrong?

Ubuntu - run bash script on startup with visible terminal

I am wanting to run a bash script on startup in Ubuntu 20.04 with the terminal visible. The test.sh file is located at /usr/bin/test.sh. I can get the test.sh file to run at startup but not in a visible terminal window.
Contents of test.sh:
#! /bin/bash
echo "hello";
I can not get it to work, I have tried (individually):
Crontab (with and without the '&' and with/without "sudo")
#reboot bash test.sh &
#reboot /usr/bin/test.sh &
#reboot DISPLAY=:0 xterm -hold -e bash -c "bash test.sh" &
#reboot DISPLAY=:0 xterm -hold -e bash -c "bash /usr/bin/test.sh" &
Startup Applications Command
sudo bash /usr/bin/test.sh
bash /usr/bin/test.sh
/usr/bin/test.sh
Creating a Service at /etc/systemd/system/testService.service
[Unit]
Description = Test Service
[Service]
WorkingDirectory= /usr/bin
ExecStart= /usr/bin/test.sh
[Install]
WantedBy=multi-user.target
And start, enable and checked status..
systemctl start testService.service
systemctl enable testService.service
systemctl status testService.service
But failed to start.
Any help / pointing in a better direction would be appreciated!
To get a GUI terminal window to appear when you run your script:
Add to "Startup Applications" (under command):
bash test.sh
Contents of test.sh:
#! /bin/bash
DISPLAY=:0.0 xterm -hold -e bash helloWorld.sh
Contents of helloWorld.sh:
#! /bin/bash
echo "hello";
For me, this opened an XTerm terminal window upon login and ran the helloWorld.sh script.
When you start a Unix, the X server gets started at the end of the startup. And starting X clients makes only sense, when someone has logged in. So your aim "start X client when computer starts" makes no sense, because there is no X server running when you try to start the X client.
You can start X clients after login. If you use a classical installation, use .xinitrc for this. If you use a different desktop environment, use whatever this desktop environment provides you.
Gnome
KDE
XFCE

Cron job not auto runs inside a Docker container

I have a LAMP container with supervisor.
I add a simple cron
* * * * * root /bin/date >> /var/log/cron.log
from my Dockerfile
ADD ./crons/test /etc/cron.d/test
RUN chmod 0777 /etc/cron.d/test
I start cron via supervisor with a supervisor-cron.conf like this:
[program:cron]
command=/bin/bash -c "cron -f"
numprocs=1
autostart=true
autorestart=true
startretries=2
Cron starts fine and stays up and running. The strange thing is that no cronjob is running automatically [as it should] but when I execute docker exec lamp crontab /etc/cron.d/test the cron job starts and works as expected.
Am I missing something? Everywhere I have read that cron jobs are executed automatically by cron.
I solved it.
I tried both setting them up in /etc/crontab and /etc/cron.d/ .
Cron didn’t auto-start the cron jobs .
However, when I run docker exec lamp crontab /etc/cron.d/my_cronjob_file all played nice. This made me suspicious , and then I read this . So, after adding my_cronjob_file in the container [in the dockerfile] I added RUN crontab /etc/cron.d/my_cronjob_file . This essentially ‘installs’ the cronjob to the crontab table. [I don’t know the internals of cron/tab but that’s the gist I understood.] .
After that , the cron service comes up by supervisor and the cronjob runs like a charm.
This can be solved with the bash file, due to the layered architecture of the Docker, cron service doesn't get initiated with RUN/CMD/ENTRYPOINT commands.
Simply add a bash file which will initiate the cron and other services (if required)
DockerFile
FROM gradle:6.5.1-jdk11 AS build
# apt
RUN apt-get update
RUN apt-get -y install cron
# Setup cron to run every minute to print (you can add/update your cron here)
RUN touch /var/log/cron-1.log
RUN (crontab -l ; echo "* * * * * echo testing cron.... >> /var/log/cron-1.log 2>&1") | crontab
# entrypoint.sh
RUN chmod +x entrypoint.sh
CMD ["bash","entrypoint.sh"]
entrypoint.sh
#!/bin/sh
service cron start & tail -f /var/log/cron-2.log
If any other service is also required to run along with cron then add that service with & in the same command, for example: /opt/wildfly/bin/standalone.sh & service cron start & tail -f /var/log/cron-2.log
Once you will get into the docker container there you can see that testing cron.... will be getting printed every minute in file: /var/log/cron-1.log

Running Node as Cron task

I have a simple question. I try to run a Node JS program on a Cron task via a bash script.
So, on crontab -e, I made a task #reboot that execute boot.sh :
# m h dom mon dow command
#reboot bash /home/pi/boot.sh
And my bash script :
#!/bin/sh
set -e
cd /home/pi/Sites/node-raspberry-pi/
/usr/bin/git pull
node /home/pi/Sites/node-raspberry-pi/index.js 3000 # where 3000 is the argument of my program
exit 0
When I do bash /home/pi/boot.sh, it works as supposed.
What do I miss ?
Note : both crontab -e and bash /home/pi/boot.sh are exectued as pi user.
Might be that your node cannot be found when cron is running; because cron has a limited search path. Try prefixing it with wherever you have node installed, so e.g., instead of
node /home/pi/Sites/node-raspberry-pi/index.js 3000
you would get
/usr/local/bin/node /home/pi/Sites/node-raspberry-pi/index.js 3000
You can also extend the searchpath for cron, see man 5 crontab. Hope this helps..

Resources