If you want something out of the box for maintaining variable state(tables/dicts/lists/atom), then have a look at http://code.kx.com/q/cookbook/logging/
/cmdline startup
q db_main -l -p 8090
/To store state before the restart, modify .z.exit to flush to the qdb file
.z.exit:{value"\\l"}
This will not cover functions however, but ideally these shouldn't be changing anyway.
If you really need to save these you can look at saving all namespaces to disk.
.z.exit:{`:/tmp/ns set get each {x!x}`$".",/:string key`}
/on startup
{{y set x[y]}[x;]each key x} `.q`.Q _ get `:/tmp/ns
Regards,
Connor