I created a simple python script to change my ubuntu wallpaper. I want to this script run after whenever I logged in.
I tried to add command in startup application as python /bin/wallpaper_changer.py but it doesn't work.
I also tried to add a desktop entry in ~/.config/autostart/Myscript.desktop but it also doesn't work.
I also added this file path in crontab using sudo crontab -e #reboot python /bin/wallpaper_changer.py it also doesn't work.
I added entry in rc.local as python /bin/wallpaper_changer.py it also doesn't work
Maybe this link will help you. I prefer to put a start-up command in
/etc/rc.local. You can call it by typing on terminal sudo nano /etc/rc.local. You can also use crontab.
I found solution for this.
first make your source file executable.
chmod +x /path/to/your/file
after add it to cron tab.
sudo crontab -e
If it asks for any editor choose editor options.
after it add your path when # symbols end.
#reboot /path/to/your/file
Related
I've written a shell script that does several things, such as...
define environment variables
activates a python virtual environment
The issue I am having is any commands that I put in after the 2nd step does not get recognized.
I do this by the following line
bash --rcfile "<PATH_TO_VIR_ENV>/bin/activate" -i
The way I use my shell script is to first log in to the linux server then do
bash ./myScript.sh
This activates my virtual environment but doesn't do the things I would like it to do afterwards. For example source by .bashrc file so that I can use aliases stored in there.
Thank you for your time in advance!
I have to run the following set of commands after opening a windows cmd prompt
D:
cd Dev\project\backend
.\venv\Scripts\activate
python manage.py runserver
How to write a script(.bat) file for running these commands and the runserver will stay showing the output
I tried to put these commands in django.bat and double click, but a cmd window opens and within a fraction of second it closes. Dont know what happened
Instead of activating the venv then running manage.py separately, you should run directly using the python.exe from your virtual env.
It should look like this:
> D:\Dev\project\backend\venv\Scripts\python.exe D:\Dev\project\backend\manage.py runserver
I don't really know what you intent to use this for, but in windows you should run your Django project using IIS. Check out django-windowsauth, you can use it to run Django on IIS in few commands. https://github.com/danyi1212/django-windowsauth.
Just skip adding the middleware and the authentication backend of you don't need Windows Authentication on your site.
I tried to do this recently, but had to move onto something else quickly; given your question it's a nice opportunity to return to it, and I believe I've found the answer!
From what I found on my Laptop just now, the issue is with the activate line
.\venv\Scripts\activate
On Windows, this is another .bat file, and I suspect that calling it runs in a new cmd instance, rather than the current one
So I simply copied the entire contents of activate.bat and put it into my custom script, and it worked!
#echo off
cd D:\Dev\project\backend
-> Paste entire contents of activate.bat on this line <-
.\manage.py runserver
I have a bash file, it works fine when executed from terminal.
#!/bin/bash
source activate tensorflow_p36
python /home/ec2-user/abc/wsgi.py
Note: tensorflow_p36 being an in-built conda environment, does not require to be called from specific /env/bin directory. It can be activated from any directory. I think it's a feature of Amazon Deep Learning AMIs.
If I run this bash script with sudo it doesnt activate the virtual environment and works in default python environment. The python file can run in that virtual environment only.
I have tried all 3 alternatives (rc.local, .conf file, init.d config)here, also tried to use crontab as suggested here. I have also tried using supervisord to add this bash script as a program.
When the program runs from these methods, I always get the same import errors because it is using default python 3 environment which doesn't have the required dependencies.
I am working on Amazon CentOS (Deep learning AMI). Can someone please suggest a method to run this script every time system restarts?
In the rc.local, instruct root to run it as you:
su --command /path/to/bash/file --login grimlock
You can run it from your personal Crontab.
( crontab -l; printf '#reboot /path/to/bash/file\n' ) | crontab -
If you don't have a crontab there will be an error message from crontab -l but it's harmless.
crontab: no crontab for ec2-user
You just need to do this once, and the job will execute as yourself once the system comes up.
try to change source by .
. activate tensorflow_p36
python /home/ec2-user/abc/wsgi.py
also check chmod +x your path file.
So I'm trying to create some scripts that I want to run without manually specifying the interpreter each time I run it.
#!/usr/bin/python
Above is the shebang on an existing script that runs like I want it to.
Below is the shebang of a script I wrote from scratch
#!/usr/bin/python
To me they look identical, but running the second one gives me a
helloWorld.py: permission denied
Both have been created using kate, utf-8 and unix lines.
Both are identical to me.
Any ideas?
The shebang may be correct, but the script also needs execute permissions.
# Anyone can execute
chmod +x helloworld.py
# Only the file owner can execute
chmod u+x helloworld.py
You need to set the permissions of the script. Try:
chmod u+x helloWorld.py
and run again.
The issue is not the permission of /usr/bin/python but rather of the actual script.
If you are running from the command line and not passing the script name as an argument to python then the script has to be executable.
If it is not then fix using chmod chmod +x helloworld.py
I've decided that it would be good for me to move outside of my .NET bubble and start experimenting with other technologies. I have Ubuntu12 running and python2.7 and 3.2 are installed. I can run code directly in the interpreters.
I have a basic script on the filesystem called Standalone.py:
#!/usr/bin/env python3.2
import sys
print("this is a standalone script.")
When I'm at my bash prompt I type $ python3.2 Standalone.py. I get a response saying this is a standalone script. But when I type $ Standalone.py then it tells me that the command is not found.
How do I run such scripts?
Thanks for any help.
update
I changed the permissions of Standalone.py to 755. Then I ran the command:
$ ./Standalone.py
and received the message:
: No such file or directory
I then switched the permissions of Standalone.py back to 644. Then when I ran
$ ./Standalone.py
I received the message
-bash: ./Standalone.py: Permission denied
Is there something I'm missing?
You need to make the script executable using
chmod +x Standalone.py
Usually, the current directory is not searched for executable files, so you need to use
./Standalone.py
to tell the shell that the script is in the current directory.
Make sure your script file has linux newline (just \n) not windows newline (\r\n). Did you write the script on windows? This happened to me once. You should check your editor settings.
Your script should start with #!/usr/bin/python not #!/usr/bin/env python3.2
Make sure you're in the folder where your script is located you can check with ls
chmod +x Standalone.py
./Standalone.py
At first, to excecute a script it need to be executable. So you either have to do a chmod +x $file or a chmod 0740 $file. When you set the file permission to 644 you are putting the execute right away, so if gives you an error. If you are unsure of execution right and octal notation, you can use this : http://permissions-calculator.org/decode/0644/.
To really answer your question then, if you want to call the script with $file.py it needs to be in your PATH variable. You can display it with echo $PATH. Those are the directories that are searched for script to execute. So you simply need to give your script the executable right and put it in one of the directory given by your PATH.
Can you check if /usr/bin/python or /usr/bin/python3.2 exists
Execute below comamnd:
which python3.2
and then use the resulting path on top of you script.