Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I am using ubuntu and I have written a python program to clean my download folder and a shell script to execute the program. But I need to run the script manually each time. Is there any way to run the script run automatically when a new file is added to that folder? I know there is inotifywait to monitor the folder but how to make that script runs from the time of reboot with setting corn??
I don't quite understand why you run Python script, from a bash script, and that Python script does clean your downloads folder. I think it would be much easier if you run just a bash script that cleans the downloads directly instead. This script would look like this:
#!/bin/bash
/usr/bin/rm -rf /home/<your_username>/Downloads
And save it as somescript.sh. Just remember to replace <your_username> with proper value.
As to how schedule running a script, one way to do this is using crontab utility in linux. It allows you to run programs at a certain time. Some examples you may find are here: crontab question, here crontab tutorial and here about crontab. Say, for example we would like to run above script at certain hour. Firstly run:
$ sudo crontab -e
To open crontab editor. Then you need to insert the line below to schedule running somescript at 6 pm everyday:
0 18 * * * /<path_to_the_script>/somescript.sh >/dev/null 2>&1
The entry in crontab has the following form:
minutes hour day month weekday (path to the script)
If you need help to start with specifying time, you might want to look at crontab generator. Also, just make sure that the script has the right privileges i.e. can be executed etc. After you save the crontab file it will be automatically scheduled. In case you want to schedule a task to run once, you might want to look into at commandline tool about at command.
Please note that you should more carefully prepare your question to make sure that everyone understands you. Ideally, provide some examples. This way you have a better chances to receive an answer that would actually help you, so I think it's a good deal. Welcome to the community :)
In order for a program to run always on a UNIX/Linux system, you might add it to the crontab, using the five asterisks, as mentioned in this serverfault post.
The best way to proceed here is to add a rm command in the crontab, using 2</dev/null at the end, in order for error messages (in case the file does not exist), not to flood your logs.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
So I've been looking around on the web for some while now but this seems to be a tricky task.
I intended to change my default terminal on a Mint system from gnome to alacrity.
I had alacrity installed before on the same system and it seemed to be work fine.
I have not set up my root user or know the password for it so this makes this extra hard!
To change the default global behavior (e.g. pressing Crtl+Alt+T) modifying the /etc/passwd seemed reasonably to me.
This is what the last line looks now: user:x:1000:1000:User,,,:/home/user:/usr/bin/alacritty
But: If I want to open a shell now almost a thousand instances do appear once the command is triggered and after a short while the whole system crashes.
I don't know how to reset to the default setting since I need a shell and that tool is broken...
Here is what I tried so far
Try to use the shell env available at user log in: Login ends in an infinite loop
Try to open the /etc/passwd in graphical environment: Cannot modify the file (read only)
So here what I wish: Make this undone without reinstalling the operational system.
Thanks for your help and advice!
The field you are trying to change in /etc/passwd is used to set the per user shell (usually /bin/bash on Linux). The terminal emulator you want to use is can either be done with update-alternatives (system wise if you have root) on Debian based systems, or Window Manager specific configuration in general (GNOME, KDE, Xmonad etc).
Login in as root, and change the /etc/passwd file back to using a valid shell for the user in question. Not sure how you don't have a root user. If you don't have the root password then follow the normal password recovery process. Boot from a live or rescue cd. If it doesn't mount the file system for you, mount it manually then edit /mnt/etc/password (where /mnt is where the original file system was mounted). Unmount and reboot your system normally.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
So,I need to compile a code which is going to take a bunch of time to compile, at least 2-3 hours. I wanted to find a way in the bash command line, that will take a screenshoot of my screen at a specific schedule.Now,I'm going to fall asleep, and my pc has 4 hours of battery left and I'm bored to move from my bed to find my adapter which is downstairs and plug in my pc.
And If I let my pc running, it is going to be out of power before i wake up.
I've tried in my ubuntu: echo"scrot"| at thehour,
But idk why,it doesnt work.
You can use this command:
gnome-screenshot -d 5
Or:
gnome-screenshot -d 5 -f ~/Desktop/Screenshots/filename.png
The -d 5 means that the will take the screenshot in 5 seconds.
The difference between both commands is that the first will ask for you (with a GUI) to save the image in the path you want. The other it will not be necessary ask for you the path. It's saved automatically after 5 seconds.
Or you can also use the command import -window root $HOME/Desktop/filename.png
That will take a screenshot and will be saved in that path.
To can use the import command you have to install the package image-magick:
sudo apt-get install imagemagick
After you have it installed, you can schedule the time with a bash script that executes the command import -window root $HOME/Desktop/filename.png. You can use sleep to can do that.
Or you can schedule the time with KShutdown, you will have to install it too.
sudo apt install kshutdown
Then, when you have installed the program, open it and will show something like this:
You have to select the Extras option and then, if you don't have scripts in the path (in my case is: ~/.local/share/kshutdown/extras), the program will ask for you create the script.
So in that path, you will have to create a script with this content:
#!/bin/bash
import -window root $HOME/Desktop/filename.png
or
gnome-screenshot -f ~/Desktop/Screenshots/filename.png
Now, if all is correct, the program will detect that script, and you can select it.
And to finish you will have to select the time you want the script runs.
You can select a certain time, like "23:30" or you can select Time From Now to run the script in certain minutes, seconds, hours...
Finally you press ok and the program will do what you want!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I'm using Linux. I'm currently trying to set my current path location, as my home directory. Anyone have a clue as to what command I need to use?
The home directory has two different meanings, which usually are the same. The value of environment variable HOME (see environ(7)), and the field pw_dir given by password related API like getpwuid_r(3) on your current user id (obtained by getuid(2)).
At login, the HOME environment variable is set to the pw_dir and the effective and real user ids are changed.
To change your working directory to your HOME use chdir(2) on the result of getenv("HOME"). Notice that the working directory is not related to your PATH variable (which might mention .; but this could be a security issue), and each process (including your shell) has its own working directory (see also credentials(7), fork(2), execve(2), path_resolution(7), glob(7)).
To change your home directory (a very unusual requirement) you could edit -with root permissions- the /etc/passwd file carefully (see passwd(5)) then reboot your machine (or at least restart some login shell).
The bash cd builtin is doing that (changing the working directory of your shell process with the chdir system call). And when you use it without arguments you are changing your working directory to your home directory obtained by caching the result of getenv("HOME").
If performance matters that much inside some C program, you might cache (keep in some global variable, initialized once) the result of getenv("HOME") and use chdir on that.
If your question is simply about using your bash shell, just type:
cd
and that should (unless cd is badly aliased or redefined as some function) change the working directory of your shell to your home directory. It is done in a few milliseconds (so should be quick enough) at most (I can't easily think of a way to measure reliably how fast the cd shell builtin is; you could try time bash -c 'cd; pwd' or time bash -c 'cd; times' but that measures much more than just the cd and gives at most a few milliseconds on my desktop PC).
PS. the use of "quickest way" and "current path location" in your question is unclear and confusing. I strongly invite you to edit your question to improve its wording and motivate it and give more context.
I'm currently trying to set my current path location as my home directory. ? you can use EXPORT PATH.
for e.g Run below command in command prompt
export PATH=$PATH:$HOME/user
This changes happens only in current session, to make it permanent add it to .bashrc file.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I want to run a series of unix commands, one after another. If any of these commands dies for whatever reason, subsequent commands should continue to run.
For instance, I have 3 commands called "setup", "long-running-job" and "teardown". If "long-running-job" finishes with whatever exit code, or dies unexpectedly, I want to make sure "teardown" gets run in any case.
Simply concatenating all commands with semicolons doesn't seem to work. I tried running touch test.txt; ping localhost; rm test.txt in macOS Terminal, closed the terminal tab while it's running, and found that the "test.text" didn't get removed.
If you want to make sure that even if your interactive window closes that your commands keep going and that all your command happen sequentially in the order you specify then use the method in this answer: https://unix.stackexchange.com/a/47231
Basically for the command line in your question
nohup sh -c 'touch test.txt; ping localhost; rm test.txt'
That means the hang up signal sent by closing the terminal is ignored.
Have you tried GNU parallel? Seems like the ideal tool for your needs.
$> parallel ::: setup long-running-job teardown
Parallel comes with tons of options to control halts, failures, jumps, retries, etc. See the manual and the tutorial for examples.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I have a text file containing a specific date and time.
I want to be able to run a script at the time specified in that file.
How would you achieve that?
Create another script that runs in background (sort of a deamon) and checks every second if the current time is matching the time in the file?
Is there another way?
The machine is a linux server , Debian wheezy.
Thanks in advance
Look at the following:
echo "ls -l" | at 07:00
This code line executes "ls -l" at a specific time. This is an example of executing something (a command in my example) at a specific time. "at" is the command you were really looking for. You can read the specifications here:
http://manpages.ubuntu.com/manpages/precise/en/man1/at.1posix.html
http://manpages.ubuntu.com/manpages/xenial/man1/at.1posix.html
The at command exists specifically for this purpose (unlike cron which is intended for scheduling recurring tasks).
at $(cat file) </path/to/script
Cron is good for something that will run periodically, like every Saturday at 4am. There's also anacron, which works around power shutdowns, sleeps, and whatnot. As well as at.
But for a one-off solution, that doesn't require root or anything, you can just use date to compute the seconds-since-epoch of the target time as well as the present time, then use expr to find the difference, and sleep that many seconds.
Usually in Linux you use crontab for this kind of scduled tasks. But you have to specify the time when you "setup the timer" - so if you want it to be configurable in the file itself, you will have to create some mechanism to do that.
But in general, you would use for example:
30 1 * * 5 /path/to/script/script.sh
Would execute the script every Friday at 1:30 (AM)
Here:
30 is minutes
1 is hour
next 2 *'s are day of month and month (in that order) and 5 is weekday