How to get script/runner to write its PID to a file?

Hello!

I must be missing something obvious (and yet I have tried a lot of things):

how can you get script/runner to write its PID to a file (and specify the location)?

I could do it manually in the code itself, but I guess this functionality is built-in already.

Thanks! Peter