Cronjob Python3 Mac not executing - macos

I just cannot get the cronjob on my Mac to execute. I have following cronjob line:
30 05 * * 1-5 usr/bin/python3 /Users/MyMac/Desktop/hello_world.py
Which should write helloworld to a txt file. This works perfectly when executing directly from the terminal. I insert this line into the crontab file using env EDITOR=nano crontab -e, exit, it says crontab: installing new crontab and when viewing the crontabs with crontab -l it's all there. It just doesn't execute when the time comes. What am I doing wrong?

I had the same issue as you, as I commented in your original post, but now I've solved it.
Python3 isn't actually located in usr/bin/Python3, which is why you don't get it to work. The terminal doesn't run the same environmental variables as crontab does which means that even if your path to python3 is wrong it will still manage to run the script from terminal since your user profile has the correct environmental variables set already. The crontab does not have these variables so it locates your script, tries to run it but can't find python3 thus not being able to compile and run the script. Python3 is located in /usr/local/bin/python3.
30 05 * * 1-5 /usr/local/bin/python3 /Users/MyMac/Desktop/hello_world.py
Try this, this works for me. I also recommend testing a software called CronniX. It let's you edit your crontab files without using the terminal which helps a ton. You can also test the script instantly from the software making it easier to see if it works or not (if it works the script should run instantly no matter what schedule times are set for it).

I also had the same problem, one of the errors for which my crontab did not work was the path of python3
in my case the path was this: /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
30 10 * * 6 /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 /Users/myMac/Desktop/main.py
Another problem for which it does not work was the permissions, to give all the permissions to cron you must do this:
Go to system preferences, security & privacy, full disk access
See the image here
Add a new one See the image here
Go to the folder pressing "Command + shift + G" and paste
this "/usr/sbin/cron", search cron and click open. Remember to check cron See the image here
If this doesn't work try giving accessibility permissions:
Go to system preferences, security & privacy,
accessibility
See the image here
Add a new one See the image here
Go to the folder pressing "Command + shift + G" and paste
this "/usr/sbin/cron", search cron and click open. Remember to check cron See the image here
Try this, this worked for me, also remember to give permissions to your terminal.

Related

shell command fails when running from crontab

[Edit: the original title of this question was "Applescript running from crontab not accessing network?" but having found that the problem is about PATHs--not networking, or for that matter Applescript--I have tried to make it more searchable.]
I have a script which I would like to run at 9pm every night that will give me an alert if the Raspberry Pi in my basement is not responding. The script works fine, and tells me my device is up when I run it by hand:
osascript /users/nat/Code/applescript/ping-pi.scpt
It also runs from my crontab, but it tells me my device is down... which it isn't.
Here is the crontab line that worked a few minutes ago (for testing, not for 9pm):
14 15 * * * osascript /users/nat/Code/applescript/ping-pi.scpt
Here is the script:
--based on https://discussions.apple.com/thread/3833490
try
set ping to (do shell script "ping -c 2 <ddns site name here>")
display dialog "<ddns site name here> is up and running!" with icon note buttons {"OK"} default button 1
on error
-- if we get here, the ping failed
display dialog "<ddns site name here> is not responding" with icon caution buttons {"OK"} default button 1
end try
The only thing I can think of is that maybe I need to run it as root, but I don't know why that would be.
I can probably get rid of the "set ping to," but I'm just adapting code I found.
It turned out that this was not a network-access issue at all, but a PATH issue. I learned in this post that the default path for cron jobs includes just /bin and /usr/bin. which ping told me that I needed /sbin/ping, and when I edited the script accordingly, it worked.
I would imagine that this would cause many cron tasks to fail, so I'm a little surprised that it didn't come to my attention sooner.
As I mentioned above, there is no problem putting up a dialog from a cron job, and I will try removing the extra permissions I added, because I'm guessing I don't actually need any special network permissions.

Run as Administrator Bat File/ Cmd File Windows 10

I have the following cmd command:
C:\Users\spidey\AppData\Local\Programs\Python\Python39\python C:\Users\spidey\Documents\sleepScript\textdocument.py
Inside a .bat file. When I double click and run it, it runs well but when I try to right click and run as administrator it just opens the window and closes adruptly without completing the execution.
The end result of the command is a text file which is created in first case but not when I try to run it as administrator.
The reason for this to run as adminstrator is because I will be running it on cloud and so there it runs as administrator.
Here are the content of textdocument.py:
import datetime
file = open('read.txt', 'w')
file.write('Executed # ' + str(datetime.datetime.now()))
file.close()
Here is another update:
Tried to create a shortcut and setup it's advanced property as run as administrator. But that doesn't work as well.
After doing this, I tried to run the .bat file again as administrator but no effect.
As suggested in the comments, I shifted all the files to C: drive so that it's accesible to everyone. But unfortunately that didn't work as well window just opens and aprubtly closes without giving the end result. On the other hand normal running works here as well.
Here is the command:
C:\Python\Python39\python C:\uiPath\textdocument.py
After a long debugging, I realized that the command was actually running correctly. It's all that it was saving the file into the other default directory after adding in my python script:
os.chdir(path)
Where path is the path where I wanted it to store. Whereas in case of double clicking and running it, by default that path was set to the path where the file was located in.
The problem got fixed. Thank you everyone for the help!

How to run Script per shortcut

I have a Terminal cmd running that executes a function from the .bash_profile file (as described here).
From the description, I added the function to the ~/.bash_profile (can be found here /home/user/.bash_profile)
From my Terminal everything works !
But how do I create a shortcut in Mac OS that executes this Terminal cmd ??
I tried Apple Automator (--> but error: Cmd not found)
I tried Alfred (--> but same error: Cmd not found)
How can I make this Terminal cmd execute from a keyboard-shortcut ??
Do I need to place the script-functions in another file (other than bash_profile ?) - or what is here to do ?
Here is an image of the Automator trial:
Place the functions in separate files, such as suspend-script.sh, resume-script.sh, and kill-script.sh; put them in a common directory like /home/user/scripts. To keep using them in your .bash_history, do:
# in .bash_history
source "suspend-script.sh"
source "resume-script.sh"
source "kill-script.sh"
Make sure you set the executable bit on each script (chmod +x /home/user/scripts/*.sh). Then, use the following for shortcut keybindings:
/home/user/scripts/suspend-script.sh thing_to_suspend
/home/user/scripts/resume-script.sh thing_to_resume
/home/user/scripts/kill-script.sh thing_to_kill
Since it's a shortcut to a function, you have to pick one script to suspend/resume/kill (the thing* above).
For example, if you wanted to suspend/resume/kill the top process, you would use the following:
/home/user/scripts/suspend-script.sh top
/home/user/scripts/resume-script.sh top
/home/user/scripts/kill-script.sh top
Open Automator, choose Application, add a Run Shell Script action and put in your Shell command between quotes (if you have a file, you can just drag and drop it).
Other than playing it, now you can save it (as an app anywhere) and even set the icon. Then you got an app and then you can define a keyboard shortcut for it in system-preferences.app
This should work, at least it does for me.
make sure your script is executable: sudo chmod u+x <your-scriptname>
and also add the filename extension .command to your filename. So the name of the script should be filename.command.
When you then click the script in Finder, it gets executed.
I hope that works!

Open an ipython notebook via double-click on osx

I've downloaded a couple of ipython notebooks, and I'd like to open them in browser tabs without navigating to the directory I've downloaded them to and running ipython notebook notebook_name.ipynb.
I realize this probably means I'm lazy, but it seems like a common use case to me. Am I missing something obvious?
Use Pineapple application for opening and working on your IPython/Jupyter notebooks. It is pretty cool.
Update:
Now there is nteract, which is a new jupyter-like Desktop app. After installing it, make it the default app for opening .ipynb files. Then just double-click any notebook to start it right away.
I have found a nice way using Automator (Oct. 2017; with information from here):
open Automator and create a new Application
to add Run Shell Script drag and drop it from the list; might need these settings Shell: /bin/bash and Pass input: as arguments
insert the code below; if necessary adjust the path to jupyter
Code
#!/bin/sh
variable="'$1'"
the_script='tell application "terminal" to do script "/usr/local/bin/jupyter notebook '
osascript -e "${the_script}${variable}\""
save the script as an application (!)
try to open a .ipynb file and change the default app to this newly created one.
notes
This will open a terminal and run the jupyter notebook command, such that you can interrupt and stop the notebook-server from there.
Also note that you cannot test the app like that in Automator, but need to add the Get Specified Finder Items and insert some test notebook there (just for testing purposes).
As pointed out in the comments, there are two more notes:
To avoid spamming your browser history with notebooks, you can start your notebooks in incognito/private mode: Run jupyter notebook in incognito window
If you want to run notebooks in one server and don't mind an extra tool, Sachit Nagpal has pointed out (thank you), that one could also use nbopen. To use this workflow just replace "/usr/local/bin/jupyter notebook ' with "nbopen '. Any other tool should work alike.
pip install nbopen.
open Automator, create new Application
Drag'n drop Run Shell Script
Change Pass input to as arguments
Copy/paste this script:
variable="'$1'"
the_script='tell application "terminal" to do script "nbopen '
osascript -e "${the_script}${variable}\""
Save the new application to Applications directory as nb_open
Right click any ipynb file and select "Open with > Other" and select the nb_open in the Applications folder. Don't forget to check "Always Open With".
Select an ipynb file, get info (command + i) > Open With (select nb_open if not selected already) > Click Change All.... Done.
The application posted here worked pretty well for me: http://bioequity.org/2013/11/16/ipynbviewer/
You also need to download iTerm2, as described on that page.
Note that this doesn't work if there are spaces in the filename, but you can modify it so that it works with spaces in the filename. Control-click on the iPyNbViewer.app and select "Show package contents". Edit the file Contents/Resources/Scripts/main.scpt. Change three instances of "POSIX path" to "quoted form of POSIX path". Now it will work with spaces in the filename.
To set all of your .ipynb files to open with the app, you'll need to Get Info (command-I) on one of the files and select the iPyNbViewer app to open all .ipynb files.
It would be great if this was the default behavior of double-clicking on an iPython notebook file...
I came up with a way of doing it on Ubuntu. (It works for me but I can take no responsibility). It's explained here. In a nutshell, you need to create a new MIME type, then write a script that works as the app that launches them:
#!/bin/bash
netstat -tln |grep "8902"
# if not found - equals to 1, start it
if [ $? -eq 1 ]
then
ipython notebook / --no-browser --port=8902 &
sleep .5
fi
xdg-open http://localhost:8902/notebooks$1
This always opens the notebook server on port 8902, but first checks whether there is already a server running, and, if so, uses it.
Then you can use ubuntu tweak to select your script as a default application for the MIME type "IPython Notebook" you just created.
Not very elegant, but worth it, in my opinion.
PyCharm now supports Jupyter ipynb files:
which is from the documentation https://www.jetbrains.com/help/pycharm/editing-jupyter-notebook-files.html.
But I think this feature is only available in the Professional version now; hopefully it will be added to the Community version in the future.
Look at this link.
Put a bash script in the folder where you keep your ipython notebooks and simply double click it to open up a notebook instance. From the link above, the bash script has just:
path=$0 # path is path to this file
cd ${path%/*.*} # clip off the file name to get directory path and cd
ipython notebook --pylab inline
Finally, you need to chmod u+x the script to make it executable and you're done.
I have used the command line application 'nbopen' and put it in a Platypus wrapper to get drag'n drop and double click opening on Macos. 'nbopen' is downloadable using 'pip'
It works well when used as described above by DanHickstein.
Only problem with my code is that it requires the full path to the nbopen command. I know I should be able to use 'which nbopen' somehow but can't get it to work.
Heres my Platypus code:
#!/bin/bash
# Opens ipynb files in a Jupyter Notebook
# echo $1
# $1 is the path of the dropped file
/Users/robw/anaconda/bin/nbopen $1
# Based on an idea from
# https://www.quora.com/Is-there-a-straightforward-way-to-open-an-IPython-Notebook-in-Windows-by-double-clicking

Apple script to run a shell script to get around permissions

I have created a shell .command on Mac OS X that I would like to distribute. Based on my testing, you can't execute the .command file by double clicking without changing permissions first ( making executable. ) I don't want users to use the terminal and change permissions, it's too hard for them.
The shell script creates a folder structure for a project, based on where the script is. It needs to be able to run anywhere the user puts it.
The research I have done indicates that I need to use an Apple script to run the file to prompt for the user's password to get around permissions.
Any advice on how to do this?
Distribute the file either by zip or DMG - props Thilo.

Resources