7

I have a shell script which gets triggerred and runs at regular interval again and again on linux system, say every 45 mins. I need to share certain information between different runs of this shell script. What are different ways I can implement this? This is somewhat similar to interprocess communication. I dont want to use disk I/O so no file creation. Something I can create in memory and can keep it alive for some time duration e.g. 7AM to 12AM etc. During this time, the scripts runs say 20 times and uses/populates that data structure again and again. The values populated in previous run need to be used in next run by the script.

Basically I am looking for something which works like DB but faster and modifying that is less expensive operation. So I will keep my data in that DB like thing and all differnt runs of script will read/modify that thing instead of going to DB again and again. /dev/shm looks good option unless someone suggests better option.

7
  • 2
    You could use tmpfs (like /dev/shm) but that's in virtual memory and may get swapped in 45 mins if there is other usage on that system. echo $BASHPID; $(echo $BASHPID >/dev/shm/foo); cat /dev/shm/foo; rm -f /dev/shm/foo Commented Jun 14, 2018 at 4:20
  • 1
    @melpomene I dont want to use I/O operation as this job is time critical. Commented Jun 15, 2018 at 0:29
  • 5
    If it's time critical, why are you using a shell script for this? Commented Jun 15, 2018 at 3:06
  • 1
    Internal functionality is written in java. Shell script is only triggering the main functionality repeatedly at regular interval. Commented Jun 17, 2018 at 3:15
  • 3
    Sounds like a job for Redis - an in-memory datastructure server with bindings for shell, PHP, Python, C, C++, Java... Commented Jun 17, 2018 at 8:51

1 Answer 1

12

I dont want to use disk I/O so no file creation.

If you're on a Linux system, create a file in /dev/shm. Files stored in this directory are only stored in shared memory; they are not written to disk.

Depending on your system configuration, /tmp and/or /var/tmp may be mounted as a tmpfs, making them behave the same way. Your mileage may vary.

Sign up to request clarification or add additional context in comments.

2 Comments

Someone has commented above that this can get swapped in some time. Is that true? I want control over this file, creation/deletion etc.
@JamesBond If there's sufficient memory pressure on the system, yes. But that's unlikely to happen for a small file unless the system is completely out of memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.