shell script execute background but can not find - shell

I made shell script
this shell script name is torrent_alert
This code "for" infinity loop execute every 10 second(sleep 10)
So I execute this script background
a few days ago I can find my script on process list use this command
ps ax | grep torrent_alert
But since yesterday I can't find my script is working or not working
I try to reboot, but it doesn't help me
So my question is I want to know my script is alive or dead and process number
How can I solve this problem?
P.S. I use script in Ubuntu

Related

xset in BASH script does not work under Cron

To learn about BASH scripting, I set myself the objective to write a Cron script which shuts down a PC with Mint 20 when activity on the Ethernet interface dropped below a threshold over an 1 hour.
I mainly (but not exclusively) use the PC as File/DLNA Server. The script works, but now I find that it also shuts down the PC the rare times I'm using the the front end. So I want my script to verify if the screen has been blanked (As per Power Management settings)
To test the principle I included this in my script:
screenon=$(/usr/bin/xset -q | grep 'Monitor is' | cut -d "s" -f 2)}
which when run in a terminal window gives (debug: set -x)
screenon= On
but when run from cron gives. (logger)
/usr/bin/xset: unable to open display ""
I have learned about similar problems, but cannot figure out how to solve this.
My script includes:PATH=$PATH:/usr/local/bin
and my PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Thanks in advance for any help.

Continue script after reboot with crontab in a terminal [duplicate]

This question already has answers here:
How to have a Shell script continue after reboot?
(3 answers)
Closed 3 years ago.
I have a bash script to install some stuff in linux. The install script needs to be run as root. The installation process reboots twice and continues after each reboot.
I managed to manipulate the crontab to add/remove jobs to get that working. However, I would like the user to be informed if the install script has finished or not, so he/she can wait until the last reboot has finished.
The only solution I could think of was to run the crontab job in an open terminal, so the user can see the installation is still in progress.
Question 1: Is this a good solution? Any alternative?
Question 2: If the solution is good, how can I make sure a terminal is opened and the crontab job is run in that terminal?
Cron jobs are executed without any attached terminal. You'll have to create one in your cron script, and redirect all output from your script's commands to it. Maybe the simplest option is to redirect your script's output to a logfile, and open a terminal which just does tail -f <logfile>. You can then kill the terminal when your script is complete. If you're using xterm (as an example), you can do xterm -e "tail -f logfile.txt".

Crontab closing background process?

I am testing a bash script I hope to run as a cron job to scan a download log and perform labor-intensive conversions on image files. In order to run several conversions at once, the first script loops through the download log and sends the filename to the second script, which I set to run as a background process using &.
The script pair works well, but when the process is complete, I must press the enter key to return to a command prompt. This is a non-issue when I am running a test, but I am not sure if this behavior has ramifications when run as a cron job.
Will this be an issue? If so, is there a way to close the "terminal" running the first script from the crontab?
Here's a truncated form of my code:
Script 1 (to be launched by crontab):
for i in file1 file2 file3 etc
do
bash /path/to/convert.sh $i &
done
exit 0
Script 2 (convert.sh)
fileName=${1?no file given}
jpegName=$(echo $fileName | sed s/tif/jpg/g)
convert $fileName $jpegName
exit 0
Thanks for any help/assurances you can give!
you don't need script 2. you can convert it to function and put it inside script1.
Another problem is you are running convert.sh in an uncontrolled way. You cannot foresee how many processes will be created (background processes) and this may lead to severe performance overheads.
finally, if you cannot end process in normal way, you may choose to terminate it again using cron by issueing pkill script1.sh

pkill -n java works at console but not in a script

I'm trying to write a script that runs a java program "Loop", and then after 5 seconds, terminates that java program, however, it's not terminating the program when I use the "pkill" option, I'm sorry for asking such a basic question, and I have looked around the internet, but I can't find a solution. Here is my code:
#!/bin/bash
javac Loop.java
java Loop
sleep 5
pkill -n java
When I run the command pkill -n java from the terminal, as opposed to in a script, it does as I expected, why is this?
Your bash script is waiting for java to complete so you'll need to run it as a background process which will start your Loop code and then return immediately, allowing the rest of your script to run:
java Loop &
More information: http://tldp.org/LDP/abs/html/x9644.html
Since you run java Loop in foreground, the next line sleep 5 doesn't get executed until your JVM exits (which is probably never, if Loop is actually an infinite loop).
So you need to start that in background:
java Loop &
Also, for killing that specific background job (rather than killing the newest JVM), you can do:
kill $!

Delay in running a command in a shell script

I have written a shell script which includes some commands to collect various Test logs.But I want to put waiting time before a particular command so that it will run after 30 mins after starting the shell script.Anyone having the knowledge regarding it please help me out.
Thanks in advance
You can use the at command to schedule a job to run at any time:
echo "/my_path/my_script" | at now + 30 minutes
If you want a delay inside your script you can sleep to a specific time using this solution.

Resources