1

I have a cron job running that is a Scala application. I want another job, that runs forever, to do something when this one finishes. It seems to me some kind of signal (Unix/Linux signal?) has to be sent for this.

For e.g., the first job writes to a file and after it finishes the second job (which is already running) reads from that file and does something.

How should I proceed with this?

  • Wouldn't it be simpler to change the job to a script saying scala_app && other_thing? Or run the other_thing from within your Scala app? – Ixrec Jul 20 '15 at 12:20
  • It sounds like you do not quite understand how cron works. I recommend looking into having the first process spawn the second directly. – user22815 Jul 20 '15 at 12:42
  • @Snowman: Sorry, I didn't express the problem correctly. I have updated the question. – user2436425 Jul 20 '15 at 12:44
1

I think the safest way to do this is for the long-running process to listen to a socket. Once the short process is done, it connects to that socket, sends over any relevant information, and disconnects.

Socket communication to localhost should never fail, so this should not introduce any meaningful risk. Sockets are better than using files to synchronize because there is no risk another process or user will alter or delete the file, and file permissions cannot prevent a socket from working.

As with other methods, sockets allow you to send arbitrary information. It could be as simple as "hi, go do your thing" or it could be a complex serialized data structure. Whatever you need.

Finally, sockets typically work by using streams to serialize and deserialize data on both ends. Both sockets and streams are very standard and established communication methods supported by virtually all operating systems and programming environments. Even if you change your technology stack in the future or expand to work with a program written in a different language in addition to what you have today, that part should still work.

I recommend checking out the list of registered IP ports and picking one that is not in use. According to the bottom of the page, ports in the range 49,152 to 65,535 cannot be registered and should be available for any use (including this one) without interfering with standard applications (e.g. an FTP/SFTP server).

  • How does this method compare to using actors in Scala? – user2436425 Jul 20 '15 at 14:34
  • 1
    @user2436425 I am not familiar with Scala actors but it sounds like it might work. The documentation specifically mentions that processes can communicate with them, not just threads. That implies two separate programs can pass data back and forth using them. – user22815 Jul 20 '15 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.