Run a cron job now - linux

I have a shell script called import.sh . This script will be used only once and will run for atleast 2 days.
I am able to schedule a cronjob like below.
02 10 25 7 * while IFS=',' read a;do /home/$USER/import.sh $a;done < /home/$USER/input/xaa
input.sh is the shell script
xaa is the file that contains arguments.
Now I want to run this script now.
I have tried ./import.sh xaa and sh -x import.sh xaa but If I run them in a terminal then I have to leave the terminal open for the time the script runs which might take more than 2 days.
How can I schedule the job to run now and terminate as soon as it finishes.

When using the command line interface for Linux, prefixing any command with nohup prevents the command from being aborted if you log out or exit the command line interface.
So you can do something like below.
nohup ./import.sh xaa

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).

Crontab not running second command

I have the following two lines in crontab. I expect the first line to start my python script 30 seconds after boot, and the second line to kill and restart the script every two minutes.
#reboot (/bin/sleep 30; /usr/bin/python3 -u /home/pi/Desktop/file.py > /home/pi/Desktop/logfile 2>&1)
*/2 * * * * (kill $(pgrep -f 'python3 -u /home/pi/Desktop/file.py'); /usr/bin/python3 -u /home/pi/Desktop/file.py > /home/pi/Desktop/logfile 2>&1)
The script does run correctly upon boot and the script is killed two minutes later, but the script is not restarted by the second line. I don't believe it is a syntax error, because if I copy the second line directly into terminal (without the */2 * * * *), it properly kills and restarts the script. Why does this line work in terminal, but not in crontab?
Thanks in advance
I'm not sure why, but it seems that crontab will not execute any other commands in the same line after the 'kill $()' command.
I discovered this by placing printf commands to a log file before an after the kill command, but only the one before kill ended up in the log. I removed the kill command but left pgrep in its place, which resulted in the first printf text, the PID number, and the second printf text in the log.
My work around was just to place the two commands in a shell file, and have crontab run the shell. Seems to work just fine.

Run a cronjob at a specific time

I would like to run a specific script at a certain time (only once!). If I run it normally like this:
marc#Marc-Linux:~/tennis_betting_strategy1/wrappers$ Rscript write_csv2.R
It does work. I however would like to program it in a cronjob to run at 10:50 and therefore did the following:
50 10 11 05 * Rscript ~/csv_file/write_csv.R
This does not seem to work however. Any thoughts where I go wrong? These are the details of the cron package im
using:
PID COMMAND
1015 cron
My system time also checks out:
marc#Marc-Linux:~/tennis_betting_strategy1/wrappers$ date
wo mei 11 10:56:46 CEST 2016
There is a special tool for running commands only once - at.
With at you can schedule a command like this:
at 09:05 am today
at> enter you commands...
Note, you'll need the atd daemon running.
Your crontab entry looks okay, however. I'd suggest checking if the cron daemon is running(exact daemon name depends on the cron package; it could be cron, crond, or vixie-cron, for instance). One way to check if the daemon is running is to use the ps command, e.g.:
$ ps -C cron -o pid,args
PID COMMAND
306 /usr/sbin/cron
Some advices.
Read more about the PATH variable. Notice that it is set differently in interactive shells (see your ~/.bashrc) and in cron or at jobs. See also this about Rscript.
Replace your command by a shell script, e.g. in ~/bin/myrscriptjob.sh
That myrscriptjob.sh file should start with #!/bin/sh
Be sure to make that shell script executable:
chmod u+x ~/bin/myrscriptjob.sh
Add some logging in your shell script, near the start; either use logger(1) or at least some date(1) command suitably redirected, or even both:
#!/bin/sh
# file myrscriptjob.sh
/bin/date +"myrscriptjob starting %c %n" > /tmp/myrscriptjob.start
/usr/bin/logger -t myrscript job starting $$
/usr/local/bin/Rscript $HOME/csv_file/write_csv.R
in the last line above, replace /usr/local/bin/Rscript by the output of which Rscript done in some interactive terminal.
Notice that you should not use ~ (but replace them with $HOME when appropriate) in shell scripts.
Finally, use at to run your script once. If you want to run it periodically in a crontab job, give the absolute path, e.g.
5 09 11 05 * $HOME/bin/myrscriptjob.sh
and check in /tmp/myrscriptjob.start and in your system log if it has started successfully.
BTW, in your myrscriptjob.sh script, you might replace the first line #!/bin/sh with #!/bin/sh -vx (then the shell is verbose about execution, and cron or at will send you some email). See dash(1), bash(1), execve(2)
Use full path (started from /) for both Rscript and write_csv2.R. Check sample command as follows
/tmp/myscript/Rscript /tmp/myfile/write_csv2.R
Ensure you have execution permission of Rscript and write permission in folder where write_csv2.R will be created(/tmp/myfile)

Run a another within script

I have to run another terminal within script and wait for function to execute on that terminal
before proceeding next command in shell script. Scenario is as like below
script.sh
!/bin/sh
...
...
gnome-terminal --working-directory=#{image_path} -e ./test.sh # execute test.sh script on another terminal
./switch 0 0 # I have to execute this command after test.sh script gets completed on another terminal
...
...
Here problem is. test.sh script is not gets executed fully and returned back immediately to parent script and executes ./switch 0 0 command. As this ./switch 0 0 command depends on test.sh script completion, my script gets failed.
Regards
Manish B.
I'm not sure if there's a switch in gnome-terminal that can make it run synchronously, but you can use xterm for that. Xterm won't go to the background unlike other terminals:
( D=$(pwd); cd "$image_path"; xterm -e "$D/test.sh" )
./switch 0 0

Linux cronjob doesn't work (execute script)

I created a cronjob with the command crontab -e:
*/1 * * * * /var/lib/tomcat/webapps/ROOT/WEB-INF/scripts/test.sh
This file test.sh should be executed every minute. But it doesn't work.
If I run the script manually it works fine. So I think the problem is the cronjob not the script ;)
Are there any permissions or something else which block the cronjob?
Is the cronjob syntax correct?
Thx
For a start, you don't need the /1 if you want it done every minute. Just setting the minute field to * will do.
Next, you should place, as the first lines in your test script (though after the #! line if it's there):
env >/tmp/test.sh.dummy
set >>/tmp/test.sh.dummy
and see if that file shows up.
That will tell you if the script is running or not.
If it's not running, check to make sure cron itself is running:
pax> ps -ef | grep cron | grep -v grep
root 1048 1 0 08:45 ? 00:00:00 cron
(mine is).
If it is running, the most likely problem is that the environment under which cron runs your jobs is nowhere near the environment your shell gives you. Examine the differences between what was output to your /tmp/test.sh.dummy file and what your shell gives your when you execute env ; set.

Resources