How to Run a Script Every Time I Save a File in Linux

How do I run a script every time I save a file in Linux?

Linux has a subsystem call inotify which can cause filesystems to report changes in the filesystem to applications.

Your linux system probably has a package called incron which makes using inotify very easy. (Or search your available packages for any package whose description contains the word 'inotify').

Setting up incron is very much like setting up cron, except whereas cron executes a script at a specified time and date, incron executes a script whenever a specified file or directory changes.

PS. On Ubuntu (for example), incron is this package and is installed with

sudo apt-get install incron

linux (and OSX) shell command to execute every time file is saved

There is a linux kernel feature called INotify that watches the file system for any changes. It is exposed as a number of system APIs.

For scripting, there is a package called inotify-tools that gives scripting access to the notification system.

How do I save a script output to a text file with iterating filename?

1. Is it possible to save a script output to a text file with a unique file name every time i run the script?

Yes.

2. Can I put all of these (the command that will generate the output of the script, the looping of file name and the command saving the iterating file name to a text file) in one script?

Yes.

This script for example does that:

#!/bin/sh

COUNTER=1
while true; do
FILENAME="filename($COUNTER)"
if [ -e $FILENAME ]; then
COUNTER=$(($COUNTER+1))
else
break
fi
done

your_command > $FILENAME

How to run a shell script when a file or directory changes?

Use inotify-tools.

The linked Github page has a number of examples; here is one of them.

#!/bin/sh

cwd=$(pwd)

inotifywait -mr \
--timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \
-e close_write /tmp/test |
while read -r date time dir file; do
changed_abs=${dir}${file}
changed_rel=${changed_abs#"$cwd"/}

rsync --progress --relative -vrae 'ssh -p 22' "$changed_rel" \
usernam@example.com:/backup/root/dir && \
echo "At ${time} on ${date}, file $changed_abs was backed up via rsync" >&2
done

Bash: Execute script on file save?

Edited: you (the OP) mentioned you use OSX. I'm not aware of any similar tools on OSX. There is a low-level system call (inherited from BSD) called "kqueue", but you'd have to implement your own user-level tool. There is a sample application from Apple, called "Watcher", but it's proof of concept only, and doesn't do what you want.

There is another thread about this on Stack Overflow (also inconclusive).

For lack of an appropriate tool, if you're using a specific programming language, I'd advise you to look for solutions already written for it. Otherwise, I think you're stuck to polling and managing the changes yourself...

Here's my original, Linux-based answer, for archival purposes:

If you're using Linux, you might want to take a look at inotify . More specifically, you can install inotify-tools, which include inotifywait.

With it, you can monitor files and directories for a number of events, such as access, modification, opening, closing and many others. inotifywait can exit once the specified event has been detected, and so a simple loop would get you what you want:

while :; do
inotifywait -e modify /some/directory
run_test_suite
done

By the way, many programming languages and environments already have their own continuous test runners (for instance, with Python you could use tdaemon, among others).

How to Run Script File on Ubuntu Every time Ubuntu Starts

Run following command to create new cronjob entry

$ crontab -e 

It will open text editor.(For first time it will ask you to select default text editor). Add your script with @reboot keyword

@reboot /path/to/your/script.sh

Save the file. That's it.

Run multiple times a script and save outputs in multiple folders

First, run the script recursively (in all folders), and second, run 'X' repetitions of the script in each folde. I got this script to run recursively.

Instead of doing the recursion, first read the list of all folders in the root folder, then "change directory" to each and every entry in the list of folders and then execute your script.

Please find the sample code below:

#!/bin/sh

if [ ! -z "${1}" ];
then
DIR_LIST=$(ls -d "${1}/*")
for dir in "${DIR_LIST}";
do
cd ${dir}
# run your script here
done
fi

Run shell script when saving a file in Sublime Text 3

Check out SublimeOnSaveBuild. All you need to do is set up a build system like SASS Build, LESS-build, Nodejs, etc., or your own custom build system, so that it's working without intervention when you press CtrlB. Then, enter the particulars into SublimeOnSaveBuild's config file (which allows you to filter by file extension, so you only trigger it when saving .sass files, for example) and you're all set - the build will trigger each time you save a file with the specified extension.

To set up the config file, first open Preferences → Package Settings → SublimeOnSaveBuild → Settings - Default, and copy the entire contents. Close the file, and paste the contents into Preferences → Package Settings → SublimeOnSaveBuild → Settings - User, customizing anything you wish. Save the file, and things should proceed automagically from there. Setting "build_on_save": 0 will disable the plugin.

How to run a shell script at startup

The file you put in /etc/init.d/ have to be set to executable with:

chmod +x /etc/init.d/start_my_app

As pointed out by @meetamit, if it still does not run you might have to create a symbolic link to the file in /etc/rc.d/

ln -s /etc/init.d/start_my_app /etc/rc.d/

Please note that on the latest versions of Debian, this will not work as your script will have to be LSB compliant (provide at least the following actions: start, stop, restart, force-reload, and status):
https://wiki.debian.org/LSBInitScripts

As a note, you should always use the absolute path to files in your scripts instead of the relative one, it may solve unexpected issues:

/var/myscripts/start_my_app

Finally, make sure that you included the shebang on top of the file:

#!/bin/sh


Related Topics



Leave a reply



Submit