How can I schedule Ruby script execution in background? - ruby

I'm trying to run a script every minute with "SINC is not CRON". I've used the following crontab line
* * * * * ruby -X D:/xampp/htdocs/maker ./do.rb
and now every minute I get a new cmd window. How can I force Ruby or SINC to invoke these as background processes?

From this link:
"In these cases, you'll want to use
rubyw.exe. It is the same as ruby.exe
except that it does not provide
standard in, standard out, or standard
error, and does not launch a DOS shell
when run."
I'm not familiar with SINC, but something like this should work:
* * * * * rubyw -X D:/xampp/htdocs/maker ./do.rb
or
* * * * * rubyw.exe -X D:/xampp/htdocs/maker ./do.rb
FWIW this is similar to Python where you would use pythonw.exe.

Related

How do I activate cron job?

I edited the cron file on my Mac running catalina.
I used this code:
crontab -e
And then I added this line
* * * * * python3 ~/Documents/Scripts/script.py
My expectation is that it should run every minute but it hasn't run once.
Do i need to do something special to activate cron on this machine?
Updated my code based on the conversation in the comments to this
* * * * * /usr/local/bin/python3 ~/Documents/Scripts/script.py >>~/Documents/Scripts/script.log 2>&1

Why does not my cronjob work?

I'm studying how to set up ubuntu server.
Today, I learned how to use crontab on the web and I tried it in my local mac computer. (not ubuntu server)
crontab -l
* * * * * /bin/echo 'hi'
I added a very simple cron job to check if it runs correctly, but it does not run!!
I tried the following, but both do not work.
A
crontab -e
* * * * * /bin/echo 'hi'
B
crontab -e
* * * * * echo 'hi'
I'm expecting that I see 'hi' every minute on my terminal console.
What am I doing wrong?
Updated
mail
I tried mail command and I saw the list of log echoing hi.
it seems it is running but how can I see 'hi' in my terminal?
echo writes on stdout, which is normally attached to your Terminal. crontab jobs however, don't have Terminals associated with them - if you have a little think about it. What would happen if you had multiple Terminals open, how would it know which one to send your "hi" to? It doesn't.
As you are learning, it might be informative to try the following, though it is not for production quality code.
Start a Terminal, and get its associated device special file:
tty
/dev/ttys000
Now make a cronjob that writes there:
crontab -e
* * * * * echo "Hi" > /dev/ttys000

Cronjob doesn't run Python script even though it has execute permissions and a hashbang to full path of python. Works normally outside of the cronjob

My crons are working normally, I tested it with simple file outputs and bash scripts, but nothing I have tried works when it comes to executing my python script.
So this is what I did.
as per usual I added a shebang to my script
#! /usr/bin/python3.5
&
chmod +x myPythonScript.py
I added it to crontab -e and tested on various ways.
examples:
* * * * * /path/to/myPythonScript.py
and just to be sure I even did the following
* * * * * python /path/to/myPythonScript.py
* * * * * python3.5 /path/to/myPythonScript.py
* * * * * /usb/bin/python /path/to/myPythonScript.py
* * * * * /usb/bin/python3.5 /path/to/myPythonScript.py
and even did */1 * * * * and other time tests.
Nothing worked, and if I write all of these outside the cron, everything works perfectly fine every time. I even changed the SHELL in the crontab -e knowing that it will not make any difference, but just to be sure, pasted the script in /etc/cron.hourly, nope, tried with /usr/bin/env python instead of the usual shebang, tried every other dumb thing, but it did not work out.
Cronjobs worked perfectly when running a bash script, I even ran a bash script to check if my python script is up and running and to notify me about that in a text log, and if its not, to restart it and also notify me about that, my text log was full of restarts and that was because it never ran the python script, so the cron was working obviously, because it ran the bash script.
When running cron, you should always write your standard out/err to a file to see whats happening. Don't trust mail because your mail server may/maynot be setup.
So, give this a try
* * * * * python /path/to/myPythonScript.py 1>/tmp/myPythonScript.out 2>/tmp/myPythonScript.err
After 1 minute check to see if the file /tmp/myPythonScript.{out,err} exist. This will give you a clear direction of what is happening.
Again, ALWAYS write your standard out/err

Cron job with shell script

i am trying to run a shell script using cronjob after every 2 minutes. I opened my terminal then typed
crontab-e
once i execute this command i am writing my command
*/2 * * * * /home/test/test/test.sh
but i am getting an error as
E486: Pattern not found: 2 * * * *
please help as i am new to this and i don't know why it is happening.
If you give me any links and code on hwo to execute cronjob it would help.
As mentioned in comments, the following error:
E486: Pattern not found: 2 * * * *
Was caused because you were not editing properly. That is, you were saying crontab -e correctly and then you were entering in vi. Instead of going into the insert mode, you would directly type */2 * * * * /home/test/test/test.sh, which vi would try to perform as a command, which is not.
So what you have to do is to press i to enter in the write mode. And then, just then, write */2 * * * * /home/test/test/test.sh. Finally, save the file by saying :wq.
In case other problems occur in your cronjob, you may want to check the "Debugging crontab" section in https://stackoverflow.com/tags/crontab/info.
I would like to show it in more detail:
crontab -e
i
* * * * * cd path/to/your/project && command
ESC
:wq
ENTER
Please check the link in the correctly marked answer above for learning more about crontab.

cron job not working for xwindow

I have following line in crontab
*/1 * * * * xeyes
it does not show any xwindow but on the contrary
*/1 * * * * touch somefile.txt works fine
Tried to search on google but didnt get any specific answers!!
You have to tell cron where to find the X server if the command you run uses it.
So use: env DISPLAY=:0.0 xeyes or export DISPLAY=:0.0; xeyes.
Some cron implementations (Debian, Ubuntu, ...) allows to set enviroment simply in cron file.
DISPLAY=:0.0
# m h dom mon dow command
*/1 * * * * xeyes

Resources